Пример #1
0
 /**
  * Crop $source
  *
  * @param string $source                The filepath of the source image
  * @param int $xStartCoord              The x coordinate from where to start the crop
  * @param int $yStartCoord              The y coordinate from where to start the crop
  * @param int $cropWidth                The width of the new image
  * @param int $cropHeight               The height of the new image
  *
  * @return boolean $response            Returns TRUE on success, FALSE on failure
  * 
  * @throws \LogicException              Occurs if gd extension is not loaded
  * @throws \InvalidArgumentException    Occurs on unsupported file type or unreadable file source
  */
 public static function cropImage($source, $xStartCoord, $yStartCoord, $cropWidth, $cropHeight)
 {
     if (false === extension_loaded('gd')) {
         throw new \LogicException('gd extension is required');
     }
     if (false === is_readable($source)) {
         throw new \InvalidArgumentException('Enable to read source file');
     }
     $targetImage = imagecreatetruecolor($cropWidth, $cropHeight);
     $mimeType = MimeType::getInstance()->guess($source);
     switch ($mimeType) {
         case 'image/jpeg':
             $sourceImage = imagecreatefromjpeg($source);
             break;
         case 'image/png':
             $sourceImage = imagecreatefrompng($source);
             break;
         case 'image/gif':
             $sourceImage = imagecreatefromgif($source);
             break;
         default:
             throw new \InvalidArgumentException('Unsupported picture type');
     }
     imagecopy($targetImage, $sourceImage, 0, 0, $xStartCoord, $yStartCoord, $cropWidth, $cropHeight);
     switch ($mimeType) {
         case 'image/jpeg':
             $response = imagejpeg($targetImage, $source);
             break;
         case 'image/png':
             $response = imagepng($targetImage, $source);
             break;
         case 'image/gif':
             $response = imagegif($targetImage, $source);
             break;
     }
     return $response;
 }
Пример #2
0
 /**
  * Create Response object for resource.
  *
  * @param string $filename valid filepath (file exists and readable)
  *
  * @return Response
  */
 private function createResourceResponse($filename)
 {
     $response = new Response();
     $filestats = stat($filename);
     $response->headers->set('Content-Type', MimeType::getInstance()->guess($filename));
     $response->headers->set('Content-Length', $filestats['size']);
     $response->setCache(array('etag' => basename($filename), 'last_modified' => new \DateTime('@' . $filestats['mtime']), 'public' => 'public'));
     $response->setContent(file_get_contents($filename));
     return $response;
 }
Пример #3
0
 /**
  * Try to resolve thumbnail paths against $basedir
  * If succeed, base64 encodes the file.
  *
  * @param  string $baseDir The base dir to look for thumbnails.
  *
  * @return Theme           The current instance for chained operations.
  */
 public function resolveThumbnails($baseDir)
 {
     $thumbnails = $this->getThumbnails();
     foreach ($thumbnails as &$thumbnail) {
         $filename = $baseDir . DIRECTORY_SEPARATOR . $thumbnail;
         if (!is_readable($filename)) {
             continue;
         }
         $thumbnail = sprintf('data:%s;base64,%s', MimeType::getInstance()->guess($filename), base64_encode(file_get_contents($filename)));
     }
     unset($thumbnail);
     $this->thumbnails = $thumbnails;
     return $this;
 }
Пример #4
0
 /**
  * Resizes an image and saves it to the provided file path.
  *
  * @param string $source The filepath of the source image
  * @param string $dest   The filepath of the target image
  * @param int    $width
  * @param int    $height
  *
  * @return boolean Returns TRUE on success, FALSE on failure
  *
  * @throws \BackBee\Exception\BBException Occures if gd extension is not loaded
  * @throws InvalidArgumentException       Occures on unsupported file type or unreadable file source
  */
 public static function resize($source, $dest, $width, $height)
 {
     if (false === extension_loaded('gd')) {
         throw new \BackBee\Exception\BBException('gd extension is required');
     }
     if (false === is_readable($source)) {
         throw new InvalidArgumentException('Enable to read source file');
     }
     if (false === ($size = getimagesize($source))) {
         throw new InvalidArgumentException('Unsupported picture type');
     }
     $source_width = $size[0];
     $source_height = $size[1];
     $mime_type = MimeType::getInstance()->guess($source);
     switch ($mime_type) {
         case 'image/jpeg':
             $source_img = imagecreatefromjpeg($source);
             break;
         case 'image/png':
             $source_img = imagecreatefrompng($source);
             break;
         case 'image/gif':
             $source_img = imagecreatefromgif($source);
             break;
         default:
             throw new InvalidArgumentException('Unsupported picture type');
     }
     if ($source_width < $width && $source_height < $height) {
         // Picture to small, no resize
         return @copy($source, $dest);
     }
     $ratio = min($width / $source_width, $height / $source_height);
     $width = $source_width * $ratio;
     $height = $source_height * $ratio;
     $target_img = imagecreatetruecolor($width, $height);
     if ('image/jpeg' !== $mime_type) {
         // Preserve alpha
         imagecolortransparent($target_img, imagecolorallocatealpha($target_img, 0, 0, 0, 127));
         imagealphablending($target_img, false);
         imagesavealpha($target_img, true);
     }
     imagecopyresampled($target_img, $source_img, 0, 0, 0, 0, $width, $height, $source_width, $source_height);
     switch ($mime_type) {
         case 'image/jpeg':
             return imagejpeg($target_img, $dest);
             break;
         case 'image/png':
             return @imagepng($target_img, $dest);
             break;
         case 'image/gif':
             return @imagegif($target_img, $dest);
             break;
     }
 }