How to use Imagick with zf2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
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; } |
暂无评论