/** * cropImageAction * * @param string $path * @param string $extension * @param int $width * @param int $height * @access public * @return Response * * @ParamConverter("photo", class="OpiumBundle:Photo", options={"slug" = "slug"}) */ public function cropImageAction(Photo $photo, $cropWidth, $cropHeight) { $path = $photo->getPathname(); if ($cropHeight === 'auto') { $cropHeight = (int) round($cropWidth * $photo->getHeight() / $photo->getWidth()); } $writePath = $this->getWritePath($photo, $cropWidth, $cropHeight); if (file_exists($writePath)) { //return new BinaryFileResponse($writePath); } $filepath = $this->container->getParameter('photos_directory') . $path; $imagick = new Imagick($filepath); if (!in_array($imagick->getImageMimeType(), $this->container->getParameter('allowed_mime_types'))) { throw $this->createNotFoundException('Wrong file mime type'); } $imagick = $this->autoRotateImage($imagick); $crop = new \stojg\crop\CropEntropy(); $crop->setImage($imagick); $imagick = $crop->resizeAndCrop($cropWidth, $cropHeight); $imagick->setInterlaceScheme(Imagick::INTERLACE_PLANE); $imagick->writeImage($writePath); return new ImagickResponse($imagick, getimagesize($filepath)['mime']); }
public function createThumbnailImage($thumbPath, $maxWidth, $maxHeight, $fillColor = false, $cropped = false) { $thumbWidth = $maxWidth; $thumbHeight = $maxHeight; if ($cropped && extension_loaded('imagick')) { if (static::$useFaceDetection && extension_loaded('facedetect')) { $cropper = new CropFace($this->FilesystemPath); } else { $cropper = new stojg\crop\CropEntropy($this->FilesystemPath); } $croppedImage = $cropper->resizeAndCrop($thumbWidth, $thumbHeight); $croppedImage->writeimage($thumbPath); } else { // load source image $srcImage = $this->getImage(); $srcWidth = imagesx($srcImage); $srcHeight = imagesy($srcImage); // calculate if ($srcWidth && $srcHeight) { $widthRatio = $srcWidth > $maxWidth ? $maxWidth / $srcWidth : 1; $heightRatio = $srcHeight > $maxHeight ? $maxHeight / $srcHeight : 1; // crop width/height to scale size if fill disabled if ($cropped) { $ratio = max($widthRatio, $heightRatio); } else { $ratio = min($widthRatio, $heightRatio); } $scaledWidth = round($srcWidth * $ratio); $scaledHeight = round($srcHeight * $ratio); } else { $scaledWidth = $maxWidth; $scaledHeight = $maxHeight; } if (!$fillColor && !$cropped) { $thumbWidth = $scaledWidth; $thumbHeight = $scaledHeight; } // create thumbnail images $image = imagecreatetruecolor($thumbWidth, $thumbHeight); // paint fill color if ($fillColor) { // extract decimal values from hex triplet $fillColor = sscanf($fillColor, '%2x%2x%2x'); // convert to color index $fillColor = imagecolorallocate($image, $fillColor[0], $fillColor[1], $fillColor[2]); // fill background imagefill($image, 0, 0, $fillColor); } elseif ($this->MIMEType == 'image/gif' || $this->MIMEType == 'image/png') { $trans_index = imagecolortransparent($srcImage); // check if there is a specific transparent color if ($trans_index >= 0 && $trans_index < imagecolorstotal($srcImage)) { $trans_color = imagecolorsforindex($srcImage, $trans_index); // allocate in thumbnail $trans_index = imagecolorallocate($image, $trans_color['red'], $trans_color['green'], $trans_color['blue']); // fill background imagefill($image, 0, 0, $trans_index); imagecolortransparent($image, $trans_index); } elseif ($this->MIMEType == 'image/png') { imagealphablending($image, false); $trans_color = imagecolorallocatealpha($image, 0, 0, 0, 127); imagefill($image, 0, 0, $trans_color); imagesavealpha($image, true); } /* $trans_index = imagecolorallocate($image, 218, 0, 245); ImageColorTransparent($image, $background); // make the new temp image all transparent imagealphablending($image, false); // turn off the alpha blending to keep the alpha channel */ } // resize photo to thumbnail if ($cropped) { imagecopyresampled($image, $srcImage, ($thumbWidth - $scaledWidth) / 2, ($thumbHeight - $scaledHeight) / 2, 0, 0, $scaledWidth, $scaledHeight, $srcWidth, $srcHeight); } else { imagecopyresampled($image, $srcImage, round(($thumbWidth - $scaledWidth) / 2), round(($thumbHeight - $scaledHeight) / 2), 0, 0, $scaledWidth, $scaledHeight, $srcWidth, $srcHeight); } // save thumbnail to disk switch ($this->ThumbnailMIMEType) { case 'image/gif': imagegif($image, $thumbPath); break; case 'image/jpeg': imagejpeg($image, $thumbPath, static::$thumbnailJPEGCompression); break; case 'image/png': imagepng($image, $thumbPath, static::$thumbnailPNGCompression); break; default: throw new Exception('Unhandled thumbnail format'); } } chmod($thumbPath, static::$newFilePermissions); return true; }