aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorfriendica <info@friendica.com>2013-04-25 20:01:24 -0700
committerfriendica <info@friendica.com>2013-04-25 20:01:24 -0700
commit72e83f69bc3afe3646b423a19f11c99d03e3b66a (patch)
tree130a7a1d0a45f3116c5729e6d7913ffcabff0acf /include
parentd3b5e678908864d889b916ad2f976c1af16ca360 (diff)
downloadvolse-hubzilla-72e83f69bc3afe3646b423a19f11c99d03e3b66a.tar.gz
volse-hubzilla-72e83f69bc3afe3646b423a19f11c99d03e3b66a.tar.bz2
volse-hubzilla-72e83f69bc3afe3646b423a19f11c99d03e3b66a.zip
photo driver abstraction
Diffstat (limited to 'include')
-rwxr-xr-xinclude/items.php12
-rw-r--r--include/network.php5
-rw-r--r--include/photo/photo_driver.php (renamed from include/Photo.php)532
-rw-r--r--include/photo/photo_gd.php137
-rw-r--r--include/photo/photo_imagick.php173
-rw-r--r--include/photos.php4
-rw-r--r--include/zot.php2
7 files changed, 410 insertions, 455 deletions
diff --git a/include/items.php b/include/items.php
index 088aa11a1..2a85556df 100755
--- a/include/items.php
+++ b/include/items.php
@@ -3,7 +3,7 @@
require_once('include/bbcode.php');
require_once('include/oembed.php');
require_once('include/crypto.php');
-require_once('include/Photo.php');
+require_once('include/photo/photo_driver.php');
require_once('include/permissions.php');
@@ -2433,7 +2433,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
if((is_array($contact)) && ($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar_date'])) {
logger('consume_feed: Updating photo for ' . $contact['name']);
- require_once("Photo.php");
+
$photo_failure = false;
$have_photo = false;
@@ -2454,7 +2454,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
$type = guess_image_type($photo_url,true);
- $img = new Photo($img_str, $type);
+ $img = photo_factory($img_str, $type);
if($img->is_valid()) {
if($have_photo) {
q("DELETE FROM `photo` WHERE `resource_id` = '%s' AND `contact-id` = %d AND `uid` = %d",
@@ -2979,7 +2979,7 @@ function local_delivery($importer,$data) {
if(($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $importer['avatar_date'])) {
logger('local_delivery: Updating photo for ' . $importer['name']);
- require_once("Photo.php");
+
$photo_failure = false;
$have_photo = false;
@@ -3000,7 +3000,7 @@ function local_delivery($importer,$data) {
$type = guess_image_type($photo_url,true);
- $img = new Photo($img_str, $type);
+ $img = photo_factory($img_str, $type);
if($img->is_valid()) {
if($have_photo) {
q("DELETE FROM `photo` WHERE `resource_id` = '%s' AND `contact-id` = %d AND `uid` = %d",
@@ -4210,7 +4210,7 @@ function fix_private_photos($s, $uid, $item = null, $cid = 0) {
$width = intval($match[1]);
$height = intval($match[2]);
- $ph = new Photo($data, $type);
+ $ph = photo_factory($data, $type);
if($ph->is_valid()) {
$ph->scaleImage(max($width, $height));
$data = $ph->imageString();
diff --git a/include/network.php b/include/network.php
index a70207be2..f750fcfd8 100644
--- a/include/network.php
+++ b/include/network.php
@@ -1051,7 +1051,8 @@ function scale_external_images($s, $include_link = true, $scale_replace = false)
$matches = null;
$c = preg_match_all('/\[img.*?\](.*?)\[\/img\]/ism',$s,$matches,PREG_SET_ORDER);
if($c) {
- require_once('include/Photo.php');
+ require_once('include/photo/photo_driver.php');
+
foreach($matches as $mtch) {
logger('scale_external_image: ' . $mtch[1]);
@@ -1080,7 +1081,7 @@ function scale_external_images($s, $include_link = true, $scale_replace = false)
$type = guess_image_type($mtch[1],true);
if($i) {
- $ph = new Photo($i, $type);
+ $ph = photo_factory($i, $type);
if($ph->is_valid()) {
$orig_width = $ph->getWidth();
$orig_height = $ph->getHeight();
diff --git a/include/Photo.php b/include/photo/photo_driver.php
index 5882f39f5..27237c530 100644
--- a/include/Photo.php
+++ b/include/photo/photo_driver.php
@@ -1,271 +1,92 @@
<?php /** @file */
+function photo_factory($data, $type = null) {
+ $ph = null;
-abstract class photo_driver {
-
- private $image;
- private $width;
- private $height;
- private $valid;
- private $type;
- private $types;
-
-
- abstract function supportedTypes();
- abstract function load();
- abstract function destroy();
- abstract function getWidth();
- abstract function getHeight();
-
- abstract function getImage();
-
- abstract function getType();
-
- abstract function getExt();
-
- abstract function scaleImage($max);
-
- abstract function rotate($degrees);
-
- abstract function flip($horiz = true, $vert = false);
-
- abstract function scaleImageUp($min);
-
- abstract function scaleImageSquare($dim);
-
- abstract function cropImage($max,$x,$y,$w,$h);
-
- abstract function imageString();
-
- public function saveImage($path) {
- if(!$this->is_valid())
- return FALSE;
- file_put_contents($path, $this->imageString());
+ if(class_exists('Imagick')) {
+ require_once('include/photo/photo_imagick.php');
+ $ph = new photo_imagick($data,$type);
+ }
+ else {
+ require_once('include/photo/photo_gd.php');
+ $ph = new photo_gd($data,$type);
}
- public function orient($filename) {
-
- /**
- * This function is a bit unusual, because it is operating on a file, but you must
- * first create an image from that file to initialise the type and check validity
- * of the image.
- */
-
- if(! $this->is_valid())
- return FALSE;
-
- if((! function_exists('exif_read_data')) || ($this->getType() !== 'image/jpeg'))
- return;
-
- $exif = @exif_read_data($filename);
- if($exif) {
- $ort = $exif['Orientation'];
-
- switch($ort)
- {
- case 1: // nothing
- break;
-
- case 2: // horizontal flip
- $this->flip();
- break;
+ return $ph;
+}
- case 3: // 180 rotate left
- $this->rotate(180);
- break;
- case 4: // vertical flip
- $this->flip(false, true);
- break;
- case 5: // vertical flip + 90 rotate right
- $this->flip(false, true);
- $this->rotate(-90);
- break;
- case 6: // 90 rotate right
- $this->rotate(-90);
- break;
+abstract class photo_driver {
- case 7: // horizontal flip + 90 rotate right
- $this->flip();
- $this->rotate(-90);
- break;
-
- case 8: // 90 rotate left
- $this->rotate(90);
- break;
- }
- }
- }
-}
+ protected $image;
+ protected $width;
+ protected $height;
+ protected $valid;
+ protected $type;
+ protected $types;
+ abstract function supportedTypes();
+ abstract function load($data,$type);
+ abstract function destroy();
+ abstract function setDimensions();
+ abstract function getImage();
+ abstract function doScaleImage($new_width,$new_height);
-class Photo {
+ abstract function rotate($degrees);
- private $image;
+ abstract function flip($horiz = true, $vert = false);
- /**
- * Put back gd stuff, not everybody have Imagick
- */
- private $imagick;
- private $width;
- private $height;
- private $valid;
- private $type;
- private $types;
+ abstract function cropImage($max,$x,$y,$w,$h);
- /**
- * supported mimetypes and corresponding file extensions
- */
- static function supportedTypes() {
- if(class_exists('Imagick')) {
- /**
- * Imagick::queryFormats won't help us a lot there...
- * At least, not yet, other parts of friendica uses this array
- */
- $t = array(
- 'image/jpeg' => 'jpg',
- 'image/png' => 'png',
- 'image/gif' => 'gif'
- );
- } else {
- $t = array();
- $t['image/jpeg'] ='jpg';
- if (imagetypes() & IMG_PNG) $t['image/png'] = 'png';
- }
+ abstract function imageString();
- return $t;
- }
- public function __construct($data, $type=null) {
- $this->imagick = class_exists('Imagick');
+ public function __construct($data, $type='') {
$this->types = $this->supportedTypes();
- if (!array_key_exists($type,$this->types)){
+ if (! array_key_exists($type,$this->types)){
$type='image/jpeg';
}
$this->type = $type;
-
- if($this->is_imagick()) {
- $this->image = new Imagick();
- $this->image->readImageBlob($data);
-
- /**
- * Setup the image to the format it will be saved to
- */
- $map = $this->get_FormatsMap();
- $format = $map[$type];
- $this->image->setFormat($format);
-
- // Always coalesce, if it is not a multi-frame image it won't hurt anyway
- $this->image = $this->image->coalesceImages();
-
- /**
- * setup the compression here, so we'll do it only once
- */
- switch($this->getType()){
- case "image/png":
- $quality = get_config('system','png_quality');
- if((! $quality) || ($quality > 9))
- $quality = PNG_QUALITY;
- /**
- * From http://www.imagemagick.org/script/command-line-options.php#quality:
- *
- * 'For the MNG and PNG image formats, the quality value sets
- * the zlib compression level (quality / 10) and filter-type (quality % 10).
- * The default PNG "quality" is 75, which means compression level 7 with adaptive PNG filtering,
- * unless the image has a color map, in which case it means compression level 7 with no PNG filtering'
- */
- $quality = $quality * 10;
- $this->image->setCompressionQuality($quality);
- break;
- case "image/jpeg":
- $quality = get_config('system','jpeg_quality');
- if((! $quality) || ($quality > 100))
- $quality = JPEG_QUALITY;
- $this->image->setCompressionQuality($quality);
- }
- } else {
- $this->valid = false;
- $this->image = @imagecreatefromstring($data);
- if($this->image !== FALSE) {
- $this->width = imagesx($this->image);
- $this->height = imagesy($this->image);
- $this->valid = true;
- imagealphablending($this->image, false);
- imagesavealpha($this->image, true);
- }
- }
+ $this->valid = false;
+ $this->load($data,$type);
}
public function __destruct() {
- if($this->image) {
- if($this->is_imagick()) {
- $this->image->clear();
- $this->image->destroy();
- return;
- }
- imagedestroy($this->image);
- }
- }
-
- public function is_imagick() {
- return $this->imagick;
- }
-
- /**
- * Maps Mime types to Imagick formats
- */
- public function get_FormatsMap() {
- $m = array(
- 'image/jpeg' => 'JPG',
- 'image/png' => 'PNG',
- 'image/gif' => 'GIF'
- );
- return $m;
+ if($this->is_valid())
+ $this->destroy();
}
public function is_valid() {
- if($this->is_imagick())
- return ($this->image !== FALSE);
return $this->valid;
}
public function getWidth() {
if(!$this->is_valid())
return FALSE;
-
- if($this->is_imagick())
- return $this->image->getImageWidth();
return $this->width;
}
public function getHeight() {
if(!$this->is_valid())
return FALSE;
-
- if($this->is_imagick())
- return $this->image->getImageHeight();
return $this->height;
}
- public function getImage() {
+
+ public function saveImage($path) {
if(!$this->is_valid())
return FALSE;
-
- if($this->is_imagick()) {
- /* Clean it */
- $this->image = $this->image->deconstructImages();
- return $this->image;
- }
- return $this->image;
+ file_put_contents($path, $this->imageString());
}
+
public function getType() {
if(!$this->is_valid())
return FALSE;
@@ -339,95 +160,72 @@ class Photo {
}
}
}
+ $this->doScaleImage($dest_width,$dest_height);
+ }
+ public function scaleImageUp($min) {
+ if(!$this->is_valid())
+ return FALSE;
- if($this->is_imagick()) {
- /**
- * If it is not animated, there will be only one iteration here,
- * so don't bother checking
- */
- // Don't forget to go back to the first frame
- $this->image->setFirstIterator();
- do {
- // FIXME - implement horizantal bias for scaling as in followin GD functions
- // to allow very tall images to be constrained only horizontally.
+ $width = $this->width;
+ $height = $this->height;
- $this->image->scaleImage($dest_width, $dest_height);
- } while ($this->image->nextImage());
+ $dest_width = $dest_height = 0;
- // FIXME - also we need to copy the new dimensions to $this->height, $this->width as other functions
- // may rely on it.
+ if((! $width)|| (! $height))
+ return FALSE;
- return;
+ if($width < $min && $height < $min) {
+ if($width > $height) {
+ $dest_width = $min;
+ $dest_height = intval(( $height * $min ) / $width);
+ }
+ else {
+ $dest_width = intval(( $width * $min ) / $height);
+ $dest_height = $min;
+ }
}
-
-
- $dest = imagecreatetruecolor( $dest_width, $dest_height );
- imagealphablending($dest, false);
- imagesavealpha($dest, true);
- if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
- imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
- if($this->image)
- imagedestroy($this->image);
- $this->image = $dest;
- $this->width = imagesx($this->image);
- $this->height = imagesy($this->image);
+ else {
+ if( $width < $min ) {
+ $dest_width = $min;
+ $dest_height = intval(( $height * $min ) / $width);
+ }
+ else {
+ if( $height < $min ) {
+ $dest_width = intval(( $width * $min ) / $height);
+ $dest_height = $min;
+ }
+ else {
+ $dest_width = $width;
+ $dest_height = $height;
+ }
+ }
+ }
+ $this->doScaleImage($dest_width,$dest_height);
}
- public function rotate($degrees) {
+ public function scaleImageSquare($dim) {
if(!$this->is_valid())
return FALSE;
-
- if($this->is_imagick()) {
- $this->image->setFirstIterator();
- do {
- $this->image->rotateImage(new ImagickPixel(), -$degrees); // ImageMagick rotates in the opposite direction of imagerotate()
- } while ($this->image->nextImage());
- return;
- }
-
- $this->image = imagerotate($this->image,$degrees,0);
- $this->width = imagesx($this->image);
- $this->height = imagesy($this->image);
+ $this->doScaleImage($dim,$dim);
}
- public function flip($horiz = true, $vert = false) {
- if(!$this->is_valid())
- return FALSE;
- if($this->is_imagick()) {
- $this->image->setFirstIterator();
- do {
- if($horiz) $this->image->flipImage();
- if($vert) $this->image->flopImage();
- } while ($this->image->nextImage());
- return;
- }
- $w = imagesx($this->image);
- $h = imagesy($this->image);
- $flipped = imagecreate($w, $h);
- if($horiz) {
- for ($x = 0; $x < $w; $x++) {
- imagecopy($flipped, $this->image, $x, 0, $w - $x - 1, 0, 1, $h);
- }
- }
- if($vert) {
- for ($y = 0; $y < $h; $y++) {
- imagecopy($flipped, $this->image, 0, $y, 0, $h - $y - 1, $w, 1);
- }
- }
- $this->image = $flipped;
- }
public function orient($filename) {
- // based off comment on http://php.net/manual/en/function.imagerotate.php
- if(!$this->is_valid())
+ /**
+ * This function is a bit unusual, because it is operating on a file, but you must
+ * first create an image from that file to initialise the type and check validity
+ * of the image.
+ */
+
+ if(! $this->is_valid())
return FALSE;
- if( (! function_exists('exif_read_data')) || ($this->getType() !== 'image/jpeg') )
+ if((! function_exists('exif_read_data')) || ($this->getType() !== 'image/jpeg'))
return;
$exif = @exif_read_data($filename);
@@ -473,165 +271,6 @@ class Photo {
}
-
- public function scaleImageUp($min) {
- if(!$this->is_valid())
- return FALSE;
-
-
- $width = $this->width;
- $height = $this->height;
-
- $dest_width = $dest_height = 0;
-
- if((! $width)|| (! $height))
- return FALSE;
-
- if($width < $min && $height < $min) {
- if($width > $height) {
- $dest_width = $min;
- $dest_height = intval(( $height * $min ) / $width);
- }
- else {
- $dest_width = intval(( $width * $min ) / $height);
- $dest_height = $min;
- }
- }
- else {
- if( $width < $min ) {
- $dest_width = $min;
- $dest_height = intval(( $height * $min ) / $width);
- }
- else {
- if( $height < $min ) {
- $dest_width = intval(( $width * $min ) / $height);
- $dest_height = $min;
- }
- else {
- $dest_width = $width;
- $dest_height = $height;
- }
- }
- }
-
- if($this->is_imagick())
- return $this->scaleImage($dest_width,$dest_height);
-
- $dest = imagecreatetruecolor( $dest_width, $dest_height );
- imagealphablending($dest, false);
- imagesavealpha($dest, true);
- if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
- imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
- if($this->image)
- imagedestroy($this->image);
- $this->image = $dest;
- $this->width = imagesx($this->image);
- $this->height = imagesy($this->image);
- }
-
-
-
- public function scaleImageSquare($dim) {
- if(!$this->is_valid())
- return FALSE;
-
- if($this->is_imagick()) {
- $this->image->setFirstIterator();
- do {
- $this->image->scaleImage($dim, $dim);
- } while ($this->image->nextImage());
- return;
- }
-
- $dest = imagecreatetruecolor( $dim, $dim );
- imagealphablending($dest, false);
- imagesavealpha($dest, true);
- if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
- imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
- if($this->image)
- imagedestroy($this->image);
- $this->image = $dest;
- $this->width = imagesx($this->image);
- $this->height = imagesy($this->image);
- }
-
-
- public function cropImage($max,$x,$y,$w,$h) {
- if(!$this->is_valid())
- return FALSE;
-
- if($this->is_imagick()) {
- $this->image->setFirstIterator();
- do {
- $this->image->cropImage($w, $h, $x, $y);
- /**
- * We need to remove the canva,
- * or the image is not resized to the crop:
- * http://php.net/manual/en/imagick.cropimage.php#97232
- */
- $this->image->setImagePage(0, 0, 0, 0);
- } while ($this->image->nextImage());
- return $this->scaleImage($max);
- }
-
- $dest = imagecreatetruecolor( $max, $max );
- imagealphablending($dest, false);
- imagesavealpha($dest, true);
- if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
- imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
- if($this->image)
- imagedestroy($this->image);
- $this->image = $dest;
- $this->width = imagesx($this->image);
- $this->height = imagesy($this->image);
- }
-
- public function saveImage($path) {
- if(!$this->is_valid())
- return FALSE;
-
- $string = $this->imageString();
- file_put_contents($path, $string);
- }
-
- public function imageString() {
- if(!$this->is_valid())
- return FALSE;
-
- if($this->is_imagick()) {
- /* Clean it */
- $this->image = $this->image->deconstructImages();
- $string = $this->image->getImagesBlob();
- return $string;
- }
-
- $quality = FALSE;
-
- ob_start();
-
- switch($this->getType()){
- case "image/png":
- $quality = get_config('system','png_quality');
- if((! $quality) || ($quality > 9))
- $quality = PNG_QUALITY;
- imagepng($this->image,NULL, $quality);
- break;
- case "image/jpeg":
- default:
- $quality = get_config('system','jpeg_quality');
- if((! $quality) || ($quality > 100))
- $quality = JPEG_QUALITY;
- imagejpeg($this->image,NULL,$quality);
- break;
- }
- $string = ob_get_contents();
- ob_end_clean();
-
- return $string;
- }
-
-
-
public function store($aid, $uid, $xchan, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') {
$x = q("select id from photo where `resource_id` = '%s' and uid = %d and `xchan` = '%s' and `scale` = %d limit 1",
@@ -712,15 +351,18 @@ class Photo {
}
return $r;
}
+
}
+
/**
* Guess image mimetype from filename or from Content-Type header
*
* @arg $filename string Image filename
* @arg $fromcurl boolean Check Content-Type header from curl request
*/
+
function guess_image_type($filename, $fromcurl=false) {
logger('Photo: guess_image_type: '.$filename . ($fromcurl?' from curl headers':''), LOGGER_DEBUG);
$type = null;
diff --git a/include/photo/photo_gd.php b/include/photo/photo_gd.php
new file mode 100644
index 000000000..a32bd2816
--- /dev/null
+++ b/include/photo/photo_gd.php
@@ -0,0 +1,137 @@
+<?php /** @file */
+
+
+require_once('include/photo/photo_driver.php');
+
+
+class photo_gd extends photo_driver {
+
+ function supportedTypes() {
+ $t = array();
+ $t['image/jpeg'] ='jpg';
+ if (imagetypes() & IMG_PNG) $t['image/png'] = 'png';
+
+ return $t;
+
+ }
+
+ function load($data, $type) {
+ $this->valid = false;
+ $this->image = @imagecreatefromstring($data);
+ if($this->image !== FALSE) {
+ $this->valid = true;
+ $this->setDimensions();
+ imagealphablending($this->image, false);
+ imagesavealpha($this->image, true);
+ }
+ }
+
+ function setDimensions() {
+ $this->width = imagesx($this->image);
+ $this->height = imagesy($this->image);
+ }
+
+
+ public function destroy() {
+ if($this->is_valid()) {
+ imagedestroy($this->image);
+ }
+ }
+
+ public function getImage() {
+ if(!$this->is_valid())
+ return FALSE;
+
+ return $this->image;
+ }
+
+ public function doScaleImage($dest_width,$dest_height) {
+
+ $dest = imagecreatetruecolor( $dest_width, $dest_height );
+ $width = imagesx($this->image);
+ $height = imagesy($this->image);
+
+ imagealphablending($dest, false);
+ imagesavealpha($dest, true);
+ if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
+ imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
+ if($this->image)
+ imagedestroy($this->image);
+ $this->image = $dest;
+ $this->setDimensions();
+ }
+
+ public function rotate($degrees) {
+ if(!$this->is_valid())
+ return FALSE;
+
+ $this->image = imagerotate($this->image,$degrees,0);
+ $this->setDimensions();
+ }
+
+ public function flip($horiz = true, $vert = false) {
+ if(!$this->is_valid())
+ return FALSE;
+
+ $w = imagesx($this->image);
+ $h = imagesy($this->image);
+ $flipped = imagecreate($w, $h);
+ if($horiz) {
+ for ($x = 0; $x < $w; $x++) {
+ imagecopy($flipped, $this->image, $x, 0, $w - $x - 1, 0, 1, $h);
+ }
+ }
+ if($vert) {
+ for ($y = 0; $y < $h; $y++) {
+ imagecopy($flipped, $this->image, 0, $y, 0, $h - $y - 1, $w, 1);
+ }
+ }
+ $this->image = $flipped;
+ $this->setDimensions(); // Shouldn't really be necessary
+ }
+
+ public function cropImage($max,$x,$y,$w,$h) {
+ if(!$this->is_valid())
+ return FALSE;
+
+ $dest = imagecreatetruecolor( $max, $max );
+ imagealphablending($dest, false);
+ imagesavealpha($dest, true);
+ if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
+ imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
+ if($this->image)
+ imagedestroy($this->image);
+ $this->image = $dest;
+ $this->setDimensions();
+ }
+
+ public function imageString() {
+ if(!$this->is_valid())
+ return FALSE;
+
+ $quality = FALSE;
+
+ ob_start();
+
+ switch($this->getType()){
+ case "image/png":
+ $quality = get_config('system','png_quality');
+ if((! $quality) || ($quality > 9))
+ $quality = PNG_QUALITY;
+ imagepng($this->image,NULL, $quality);
+ break;
+ case "image/jpeg":
+ default:
+ $quality = get_config('system','jpeg_quality');
+ if((! $quality) || ($quality > 100))
+ $quality = JPEG_QUALITY;
+ imagejpeg($this->image,NULL,$quality);
+ break;
+ }
+ $string = ob_get_contents();
+ ob_end_clean();
+
+ return $string;
+ }
+
+} \ No newline at end of file
diff --git a/include/photo/photo_imagick.php b/include/photo/photo_imagick.php
new file mode 100644
index 000000000..7282898b4
--- /dev/null
+++ b/include/photo/photo_imagick.php
@@ -0,0 +1,173 @@
+<?php /** @file */
+
+
+require_once('include/photo/photo_driver.php');
+
+
+class photo_imagick extends photo_driver {
+
+
+ function supportedTypes() {
+ return array(
+ 'image/jpeg' => 'jpg',
+ 'image/png' => 'png',
+ 'image/gif' => 'gif'
+ );
+ }
+
+ public function get_FormatsMap() {
+ return array(
+ 'image/jpeg' => 'JPG',
+ 'image/png' => 'PNG',
+ 'image/gif' => 'GIF'
+ );
+ }
+
+
+ function load($data, $type) {
+ $this->valid = false;
+ $this->image = new Imagick();
+ $this->image->readImageBlob($data);
+
+
+ /**
+ * Setup the image to the format it will be saved to
+ */
+
+ $map = $this->get_FormatsMap();
+ $format = $map[$type];
+
+ if($this->image) {
+ $this->image->setFormat($format);
+
+ // Always coalesce, if it is not a multi-frame image it won't hurt anyway
+ $this->image = $this->image->coalesceImages();
+
+
+ $this->valid = true;
+ $this->setDimensions();
+
+ /**
+ * setup the compression here, so we'll do it only once
+ */
+ switch($this->getType()) {
+ case "image/png":
+ $quality = get_config('system','png_quality');
+ if((! $quality) || ($quality > 9))
+ $quality = PNG_QUALITY;
+ /**
+ * From http://www.imagemagick.org/script/command-line-options.php#quality:
+ *
+ * 'For the MNG and PNG image formats, the quality value sets
+ * the zlib compression level (quality / 10) and filter-type (quality % 10).
+ * The default PNG "quality" is 75, which means compression level 7 with adaptive PNG filtering,
+ * unless the image has a color map, in which case it means compression level 7 with no PNG filtering'
+ */
+ $quality = $quality * 10;
+ $this->image->setCompressionQuality($quality);
+ break;
+ case "image/jpeg":
+ $quality = get_config('system','jpeg_quality');
+ if((! $quality) || ($quality > 100))
+ $quality = JPEG_QUALITY;
+ $this->image->setCompressionQuality($quality);
+
+ default:
+ break;
+ }
+ }
+ }
+
+ public function destroy() {
+ if($this->is_valid()) {
+ $this->image->clear();
+ $this->image->destroy();
+ }
+ }
+
+
+ public function setDimensions() {
+ $this->width = $this->image->getImageWidth();
+ $this->height = $this->image->getImageHeight();
+ }
+
+
+ public function getImage() {
+ if(!$this->is_valid())
+ return FALSE;
+
+ $this->image = $this->image->deconstructImages();
+ return $this->image;
+ }
+
+ public function doScaleImage($dest_width,$dest_height) {
+
+ /**
+ * If it is not animated, there will be only one iteration here,
+ * so don't bother checking
+ */
+ // Don't forget to go back to the first frame
+ $this->image->setFirstIterator();
+ do {
+ $this->image->scaleImage($dest_width, $dest_height);
+ } while ($this->image->nextImage());
+
+ $this->setDimensions();
+ }
+
+ public function rotate($degrees) {
+ if(!$this->is_valid())
+ return FALSE;
+
+ $this->image->setFirstIterator();
+ do {
+ // ImageMagick rotates in the opposite direction of imagerotate()
+ $this->image->rotateImage(new ImagickPixel(), -$degrees);
+ } while ($this->image->nextImage());
+
+ $this->setDimensions();
+ }
+
+ public function flip($horiz = true, $vert = false) {
+ if(!$this->is_valid())
+ return FALSE;
+
+ $this->image->setFirstIterator();
+ do {
+ if($horiz) $this->image->flipImage();
+ if($vert) $this->image->flopImage();
+ } while ($this->image->nextImage());
+
+ $this->setDimensions(); // Shouldn't really be necessary
+ }
+
+ public function cropImage($max,$x,$y,$w,$h) {
+ if(!$this->is_valid())
+ return FALSE;
+
+ $this->image->setFirstIterator();
+ do {
+ $this->image->cropImage($w, $h, $x, $y);
+ /**
+ * We need to remove the canva,
+ * or the image is not resized to the crop:
+ * http://php.net/manual/en/imagick.cropimage.php#97232
+ */
+ $this->image->setImagePage(0, 0, 0, 0);
+ } while ($this->image->nextImage());
+
+ $this->doScaleImage($max);
+ }
+
+ public function imageString() {
+ if(!$this->is_valid())
+ return FALSE;
+
+ /* Clean it */
+ $this->image = $this->image->deconstructImages();
+ return $this->image->getImagesBlob();
+ }
+
+
+
+} \ No newline at end of file
diff --git a/include/photos.php b/include/photos.php
index 123cd0659..fae01ba80 100644
--- a/include/photos.php
+++ b/include/photos.php
@@ -2,6 +2,8 @@
require_once('include/permissions.php');
require_once('include/items.php');
+require_once('include/photo/photo_driver.php');
+
function photo_upload($channel, $observer, $args) {
@@ -116,7 +118,7 @@ function photo_upload($channel, $observer, $args) {
}
- $ph = new Photo($imagedata, $type);
+ $ph = photo_factory($imagedata, $type);
if(! $ph->is_valid()) {
$ret['message'] = t('Unable to process image');
diff --git a/include/zot.php b/include/zot.php
index e8fba603a..9addf80cf 100644
--- a/include/zot.php
+++ b/include/zot.php
@@ -563,7 +563,7 @@ function import_xchan($arr) {
if($import_photos) {
- require_once("Photo.php");
+ require_once('include/photo/photo_driver.php');
$photos = import_profile_photo($arr['photo'],$xchan_hash);
$r = q("update xchan set xchan_photo_date = '%s', xchan_photo_l = '%s', xchan_photo_m = '%s', xchan_photo_s = '%s', xchan_photo_mimetype = '%s'