Example #1
0
 /**
  * Creates a Thumbnail if file type is supported.
  * Supported types are: JPEG, PNG, GIF, WBMP, and GD2.
  *
  * @param File    $fileObject           FileObject to add properties
  * @param string  $httpPathToMediaDir   Http path to file
  * @param string  $pathToMediaDirectory LOCAL Path to file (without backslash at the end)
  * @param string  $fileName             Name of the image
  * @param string  $fileExtension        Fileextension 
  * @param integer $width                Width of thumbnail, if omitted, size will be proportional to height
  * @param integer $heigt                Height of thumbnail, if omitted, size will be proportional to width
  *
  * @throws ThumbnailCreationFailedException             If thumbnail creation fails
  * @throws InvalidArgumentException     If both, height and width, are omitted or the file format is not supported
  */
 private static function createThumbnailWithGd(File &$fileObject, $httpPathToMediaDir, $pathToMediaDirectory, $fileName, $fileExtension, $width = null, $height = null)
 {
     if ($width === null && $height === null) {
         throw new InvalidArgumentException('Either width or height must be provided');
     }
     if ($fileExtension == 'jpg' || $fileExtension == 'jpeg' && function_exists('imagecreatefromjpeg')) {
         $img = imagecreatefromjpeg(sprintf('%s/%s.%s', $pathToMediaDirectory, $fileName, $fileExtension));
     } else {
         if ($fileExtension == 'png' && function_exists('imagecreatefrompng')) {
             $img = imagecreatefrompng(sprintf('%s/%s.%s', $pathToMediaDirectory, $fileName, $fileExtension));
         } else {
             if ($fileExtension == 'gif' && function_exists('imagecreatefromgif')) {
                 $img = imagecreatefromgif(sprintf('%s/%s.%s', $pathToMediaDirectory, $fileName, $fileExtension));
             } else {
                 throw new ThumbnailCreationFailedException('No thumbnail could be created for the file format');
             }
         }
     }
     // image is corrupt, in the wrong file type or contains wrong data
     if ($img === false) {
         throw new ThumbnailCreationFailedException('No thumbnail could be created for the file format');
     }
     $imgWidth = imagesx($img);
     $imgHeight = imagesy($img);
     $fileObject->setDimensions(sprintf('%sx%s', $imgWidth, $imgHeight));
     // calculate thumbnail dimensions
     if ($width && !$height) {
         $newHeight = floor($imgHeight * ($width / $imgWidth));
         $newWidth = $width;
     } else {
         if (!$width && $height) {
             $newHeight = $height;
             $newWidth = floor($imgWidth * ($height / $imgHeight));
         }
     }
     // create a new temporary image
     $tmpImg = imagecreatetruecolor($newWidth, $newHeight);
     // check resource identifier
     if ($tmpImg === false) {
         throw new ThumbnailCreationFailedException('Thumbnail creation failed on temporary image');
     }
     // copy and resize old image into new image
     $ret = imagecopyresized($tmpImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $imgWidth, $imgHeight);
     if ($ret === false) {
         throw new ThumbnailCreationFailedException('Thumbnail creation failed on resizing');
     }
     // save thumbnail into a file, set image quality to 100
     $ret = imagejpeg($tmpImg, sprintf('%s/%sx%s-thumbnail-%s.%s', $pathToMediaDirectory, $newWidth, $newHeight, $fileName, $fileExtension), 100);
     if ($ret === false) {
         throw new ThumbnailCreationFailedException('Thumbnail creation failed on saving thumbnail');
     }
     $fileObject->setThumbnailLink(sprintf('%s/%sx%s-thumbnail-%s.%s', $httpPathToMediaDir, $newWidth, $newHeight, $fileName, $fileExtension));
     $fileObject->setLocalThumbnailPath(sprintf('%s/%sx%s-thumbnail-%s.%s', $pathToMediaDirectory, $newWidth, $newHeight, $fileName, $fileExtension));
     // free memory
     imagedestroy($tmpImg);
 }