Example #1
0
 /**
  * @return boolean Returns true when successful.
  */
 private function createThumb($url, $filename, $type, $width, $height)
 {
     // Call plugins
     Plugins::get()->activate(__METHOD__, 0, func_get_args());
     // Quality of thumbnails
     $quality = 90;
     // Size of the thumbnail
     $newWidth = 200;
     $newHeight = 200;
     $photoName = explode('.', $filename);
     $newUrl = LYCHEE_UPLOADS_THUMB . $photoName[0] . '.jpeg';
     $newUrl2x = LYCHEE_UPLOADS_THUMB . $photoName[0] . '@2x.jpeg';
     // Create thumbnails with Imagick
     if (Settings::hasImagick()) {
         // Read image
         $thumb = new Imagick();
         $thumb->readImage($url);
         $thumb->setImageCompressionQuality($quality);
         $thumb->setImageFormat('jpeg');
         // Remove metadata to save some bytes
         $thumb->stripImage();
         // Copy image for 2nd thumb version
         $thumb2x = clone $thumb;
         // Create 1st version
         $thumb->cropThumbnailImage($newWidth, $newHeight);
         $thumb->writeImage($newUrl);
         $thumb->clear();
         $thumb->destroy();
         // Create 2nd version
         $thumb2x->cropThumbnailImage($newWidth * 2, $newHeight * 2);
         $thumb2x->writeImage($newUrl2x);
         $thumb2x->clear();
         $thumb2x->destroy();
     } else {
         // Create image
         $thumb = imagecreatetruecolor($newWidth, $newHeight);
         $thumb2x = imagecreatetruecolor($newWidth * 2, $newHeight * 2);
         // Set position
         if ($width < $height) {
             $newSize = $width;
             $startWidth = 0;
             $startHeight = $height / 2 - $width / 2;
         } else {
             $newSize = $height;
             $startWidth = $width / 2 - $height / 2;
             $startHeight = 0;
         }
         // Create new image
         switch ($type) {
             case 'image/jpeg':
                 $sourceImg = imagecreatefromjpeg($url);
                 break;
             case 'image/png':
                 $sourceImg = imagecreatefrompng($url);
                 break;
             case 'image/gif':
                 $sourceImg = imagecreatefromgif($url);
                 break;
             default:
                 Log::error(Database::get(), __METHOD__, __LINE__, 'Type of photo is not supported');
                 return false;
                 break;
         }
         // Create thumb
         fastImageCopyResampled($thumb, $sourceImg, 0, 0, $startWidth, $startHeight, $newWidth, $newHeight, $newSize, $newSize);
         imagejpeg($thumb, $newUrl, $quality);
         imagedestroy($thumb);
         // Create retina thumb
         fastImageCopyResampled($thumb2x, $sourceImg, 0, 0, $startWidth, $startHeight, $newWidth * 2, $newHeight * 2, $newSize, $newSize);
         imagejpeg($thumb2x, $newUrl2x, $quality);
         imagedestroy($thumb2x);
         // Free memory
         imagedestroy($sourceImg);
     }
     // Call plugins
     Plugins::get()->activate(__METHOD__, 1, func_get_args());
     return true;
 }