X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2FPhoto.php;h=69b08f62919bc7fe81855238ff9fea847dff556c;hb=c22f65bafa08ea348d55fb1f3c05774b0f320c99;hp=b7285362414d8873a71a90f39ad46eb049901b16;hpb=36f600aaf2a9c5c39a23ded592a09d1b845b058e;p=friendica.git diff --git a/include/Photo.php b/include/Photo.php index b728536241..69b08f6291 100644 --- a/include/Photo.php +++ b/include/Photo.php @@ -46,9 +46,52 @@ class Photo { } $this->type = $type; - if($this->is_imagick()) { - $this->image = new Imagick(); - $this->image->readImageBlob($data); + if($this->is_imagick() && $this->load_data($data)) { + return true; + } else { + // Failed to load with Imagick, fallback + $this->imagick = false; + } + return $this->load_data($data); + } + + 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; + } + + private function load_data($data) { + if($this->is_imagick()) { + $this->image = new Imagick(); + try { + $this->image->readImageBlob($data); + } + catch (Exception $e) { + // Imagick couldn't use the data + return false; + } /** * Setup the image to the format it will be saved to @@ -85,45 +128,29 @@ class Photo { $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); - } - } - } - 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; - } + // The 'width' and 'height' properties are only used by non-Imagick routines. + $this->width = $this->image->getImageWidth(); + $this->height = $this->image->getImageHeight(); + $this->valid = true; + + return true; + } + + $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); + + return true; + } + + return false; + } public function is_valid() { if($this->is_imagick()) @@ -179,21 +206,8 @@ class Photo { 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 { - $this->image->resizeImage($max, $max, imagick::FILTER_LANCZOS, 1, true); - } while ($this->image->nextImage()); - return; - } - - $width = $this->width; - $height = $this->height; + $width = $this->getWidth(); + $height = $this->getHeight(); $dest_width = $dest_height = 0; @@ -201,7 +215,18 @@ class Photo { return FALSE; if($width > $max && $height > $max) { - if($width > $height) { + + // very tall image (greater than 16:9) + // constrain the width - let the height float. + + if((($height * 9) / 16) > $width) { + $dest_width = $max; + $dest_height = intval(( $height * $max ) / $width); + } + + // else constrain both dimensions + + elseif($width > $height) { $dest_width = $max; $dest_height = intval(( $height * $max ) / $width); } @@ -217,8 +242,18 @@ class Photo { } else { if( $height > $max ) { - $dest_width = intval(( $width * $max ) / $height); - $dest_height = $max; + + // very tall image (greater than 16:9) + // but width is OK - don't do anything + + if((($height * 9) / 16) > $width) { + $dest_width = $width; + $dest_height = $height; + } + else { + $dest_width = intval(( $width * $max ) / $height); + $dest_height = $max; + } } else { $dest_width = $width; @@ -228,6 +263,29 @@ class Photo { } + 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. + + $this->image->scaleImage($dest_width, $dest_height); + } while ($this->image->nextImage()); + + // These may not be necessary any more + $this->width = $this->image->getImageWidth(); + $this->height = $this->image->getImageHeight(); + + return; + } + + $dest = imagecreatetruecolor( $dest_width, $dest_height ); imagealphablending($dest, false); imagesavealpha($dest, true); @@ -247,7 +305,7 @@ class Photo { if($this->is_imagick()) { $this->image->setFirstIterator(); do { - $this->image->rotateImage(new ImagickPixel(), $degrees); + $this->image->rotateImage(new ImagickPixel(), -$degrees); // ImageMagick rotates in the opposite direction of imagerotate() } while ($this->image->nextImage()); return; } @@ -295,7 +353,11 @@ class Photo { if( (! function_exists('exif_read_data')) || ($this->getType() !== 'image/jpeg') ) return; - $exif = exif_read_data($filename); + $exif = @exif_read_data($filename); + + if(! $exif) + return; + $ort = $exif['Orientation']; switch($ort) @@ -341,11 +403,9 @@ class Photo { if(!$this->is_valid()) return FALSE; - if($this->is_imagick()) - return $this->scaleImage($min); - $width = $this->width; - $height = $this->height; + $width = $this->getWidth(); + $height = $this->getHeight(); $dest_width = $dest_height = 0; @@ -379,6 +439,8 @@ class Photo { } } + if($this->is_imagick()) + return $this->scaleImage($dest_width,$dest_height); $dest = imagecreatetruecolor( $dest_width, $dest_height ); imagealphablending($dest, false); @@ -401,7 +463,7 @@ class Photo { if($this->is_imagick()) { $this->image->setFirstIterator(); do { - $this->image->resizeImage($dim, $dim, imagick::FILTER_LANCZOS, 1, false); + $this->image->scaleImage($dim, $dim); } while ($this->image->nextImage()); return; } @@ -423,19 +485,19 @@ class Photo { 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); - } + 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); @@ -470,7 +532,7 @@ class Photo { $quality = FALSE; - ob_start() + ob_start(); switch($this->getType()){ case "image/png": @@ -522,6 +584,7 @@ class Photo { `album` = '%s', `height` = %d, `width` = %d, + `datasize` = %d, `data` = '%s', `scale` = %d, `profile` = %d, @@ -542,6 +605,7 @@ class Photo { dbesc($album), intval($this->getHeight()), intval($this->getWidth()), + dbesc(strlen($this->imageString())), dbesc($this->imageString()), intval($scale), intval($profile), @@ -554,8 +618,8 @@ class Photo { } else { $r = q("INSERT INTO `photo` - ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` ) - VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )", + ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `datasize`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` ) + VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )", intval($uid), intval($cid), dbesc($guid), @@ -567,6 +631,7 @@ class Photo { dbesc($album), intval($this->getHeight()), intval($this->getWidth()), + dbesc(strlen($this->imageString())), dbesc($this->imageString()), intval($scale), intval($profile), @@ -603,7 +668,7 @@ function guess_image_type($filename, $fromcurl=false) { } if (is_null($type)){ // Guessing from extension? Isn't that... dangerous? - if(class_exists('Imagick')) { + if(class_exists('Imagick') && file_exists($filename) && is_readable($filename)) { /** * Well, this not much better, * but at least it comes from the data inside the image, @@ -633,7 +698,7 @@ function import_profile_photo($photo,$uid,$cid) { intval($uid), intval($cid) ); - if(count($r)) { + if(count($r) && strlen($r[0]['resource-id'])) { $hash = $r[0]['resource-id']; } else {