Skip to content

Instantly share code, notes, and snippets.

@rlandas
Created November 21, 2012 22:23
Show Gist options
  • Save rlandas/4128254 to your computer and use it in GitHub Desktop.
Save rlandas/4128254 to your computer and use it in GitHub Desktop.
ZF2: How to use Imagick with zf2
// @see http://giaule.com/2012/11/18/how-to-use-imagick-with-zf2/
public function uploadAction() {
$request = $this->getRequest();
if ($request->isPost()) {
$adapter = new \Zend\File\Transfer\Adapter\Http();
$adapter->addValidator('Size', false, array('min' => UPLOAD_IMAGE_MIN_SIZE, 'max' => UPLOAD_IMAGE_MAX_SIZE));
$adapter->addValidator('MimeType', true, array('image/jpeg'));
$adapter->addValidator('Count', false, array('min' =>1, 'max' => 1));
$adapter->addValidator('IsImage', false, 'jpeg');
$adapter->setDestination(UPLOAD_IMAGE_PATH . '/' . IMAGE_PATH_DEFAULT);
foreach($adapter->getFileInfo() as $file => $info) {
if ($adapter->isValid($file)) {
$name = $adapter->getFileName($file);
$fileName = CUSTOMER_ID . '_' . time() . '_' . $info['name'];
$imageDefault = UPLOAD_IMAGE_PATH . '/' . IMAGE_PATH_DEFAULT . '/' . $fileName;
$imageMedium = UPLOAD_IMAGE_PATH . '/' . IMAGE_PATH_MEDIUM . '/' . $fileName;
$imageSmall = UPLOAD_IMAGE_PATH . '/' . IMAGE_PATH_SMALL . '/' . $fileName;
$adapter->addFilter(
new \Zend\Filter\File\Rename(array('target' => $imageDefault,
'overwrite' => true)),
null, $file
);
if ($adapter->receive($file)) {
$image = new \Imagick();
/**
* process with medium
*/
$image->readImage($imageDefault);
$imageprops = $image->getImageGeometry();
if ($imageprops['width'] <= UPLOAD_SIZE_MEDIUM_WIDTH && $imageprops['height'] <= UPLOAD_SIZE_MEDIUM_HEIGHT) {
$image->writeImage($imageMedium);
} else {
$image->resizeImage(UPLOAD_SIZE_MEDIUM_WIDTH, UPLOAD_SIZE_MEDIUM_HEIGHT, $image::FILTER_LANCZOS, 0.9, true);
$image->writeImage($imageMedium);
}
/**
* process with small
*/
$image->readImage($imageDefault);
$imageprops = $image->getImageGeometry();
if ($imageprops['width'] <= UPLOAD_SIZE_SMALL_WIDTH && $imageprops['height'] <= UPLOAD_SIZE_SMALL_HEIGHT) {
$image->writeImage($imageSmall);
} else {
$image->resizeImage(UPLOAD_SIZE_SMALL_WIDTH, UPLOAD_SIZE_SMALL_HEIGHT, $image::FILTER_LANCZOS, 0.9, true);
$image->writeImage($imageSmall);
}
}
}
}
}
die;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment