public function prepareSave(Kwf_Model_Row_Interface $row, $postData) { Kwf_Form_Field_Abstract::prepareSave($row, $postData); $value = $this->_getValueFromPostData($postData); if (is_string($value)) { $value = Zend_Json::decode($value); } if (!is_array($value)) { $value = array(); } $row->dimension = isset($value['dimension']) ? $value['dimension'] : null; $row->width = isset($value['width']) && $value['width'] ? $value['width'] : null; $row->height = isset($value['height']) && $value['height'] ? $value['height'] : null; if (isset($value['cropData'])) { $row->crop_x = isset($value['cropData']['x']) && $value['cropData']['x'] !== null ? $value['cropData']['x'] : null; $row->crop_y = isset($value['cropData']['y']) && $value['cropData']['y'] !== null ? $value['cropData']['y'] : null; $row->crop_width = isset($value['cropData']['width']) && $value['cropData']['width'] ? $value['cropData']['width'] : null; $row->crop_height = isset($value['cropData']['height']) && $value['cropData']['height'] ? $value['cropData']['height'] : null; if ($row->getParentRow('Image')) { $scaleFactor = Kwf_Media_Image::getHandyScaleFactor($row->getParentRow('Image')->getFileSource()); if ($scaleFactor != 1) { $row->crop_x = $row->crop_x * $scaleFactor; $row->crop_y = $row->crop_y * $scaleFactor; $row->crop_width = $row->crop_width * $scaleFactor; $row->crop_height = $row->crop_height * $scaleFactor; } } } else { $row->crop_x = null; $row->crop_y = null; $row->crop_width = null; $row->crop_height = null; } }
public function getFileInfo() { $ret = array('uploadId' => $this->id, 'mimeType' => $this->mime_type, 'filename' => $this->filename, 'extension' => $this->extension, 'fileSize' => $this->getFileSize(), 'hashKey' => $this->getHashKey()); if (!$this->id && is_file($this->filename)) { $ret['mimeType'] = $this->_getMimeType($this->filename); $ret['extension'] = substr(strrchr($this->filename, '.'), 1); } $size = $this->getImageDimensions(); if ($size) { $ret['image'] = true; $ret['imageHandyScaleFactor'] = Kwf_Media_Image::getHandyScaleFactor($size); if (abs($size['rotation']) == 90) { $size = array('width' => $size['height'], 'height' => $size['width']); } $ret['imageWidth'] = $size['width']; $ret['imageHeight'] = $size['height']; } else { $ret['image'] = false; } return $ret; }
public function downloadHandyAction() { $fileRow = Kwf_Model_Abstract::getInstance('Kwf_Uploads_Model')->getRow($this->_getParam('uploadId')); if (!$fileRow) { throw new Kwf_Exception("Can't find upload"); } if ($fileRow->getHashKey() != $this->_getParam('hashKey')) { throw new Kwf_Exception_AccessDenied(); } $scaleFactor = Kwf_Media_Image::getHandyScaleFactor($fileRow->getFileSource()); $outputParams = array('mimeType' => $fileRow->mime_type, 'downloadFilename' => $fileRow->filename . '.' . $fileRow->extension); $targetSize = array(600, 600, 'cover' => false); $image = Kwf_Media_Image::scale($fileRow->getFileSource(), $targetSize, $fileRow->id); $outputParams['contents'] = $image; Kwf_Media_Output::output($outputParams); }
public function previewWithCropAction() { $previewWidth = 390; $previewHeight = 184; $fileRow = Kwf_Model_Abstract::getInstance('Kwf_Uploads_Model')->getRow($this->_getParam('uploadId')); if (!$fileRow) { throw new Kwf_Exception("Can't find upload"); } if ($fileRow->getHashKey() != $this->_getParam('hashKey')) { throw new Kwf_Exception_AccessDenied(); } //Scale dimensions $dimensions = array($previewWidth, $previewHeight, 'cover' => false); $cache = Kwf_Assets_Cache::getInstance(); $cacheId = 'previewLarge_' . $fileRow->id; if (!($output = $cache->load($cacheId))) { $output = array(); $output['contents'] = Kwf_Media_Image::scale($fileRow->getFileSource(), $dimensions, $fileRow->id); $output['mimeType'] = $fileRow->mime_type; $cache->save($output, $cacheId); } $imageOriginal = new Imagick($fileRow->getFileSource()); if ($this->_getParam('cropX') == null || $this->_getParam('cropY') == null || $this->_getParam('cropWidth') == null || $this->_getParam('cropHeight') == null) { //calculate default selection $dimension = $this->_getParam('dimension'); if (!$dimension) { Kwf_Media_Output::output($output); } $dimensions = Kwc_Abstract::getSetting($this->_getParam('class'), 'dimensions'); $dimension = $dimensions[$dimension]; if (!isset($dimension['width'])) { $dimension['width'] = 0; } if (!isset($dimension['height'])) { $dimension['height'] = 0; } if (!isset($dimension['cover'])) { $dimension['cover'] = false; } if ($dimension['width'] == Kwc_Abstract_Image_Component::USER_SELECT) { $dimension['width'] = $this->_getParam('width'); } if ($dimension['height'] == Kwc_Abstract_Image_Component::USER_SELECT) { $dimension['height'] = $this->_getParam('height'); } if (!$dimension['cover']) { Kwf_Media_Output::output($output); } if ($dimension['width'] == Kwc_Abstract_Image_Component::CONTENT_WIDTH) { Kwf_Media_Output::output($output); } if ($dimension['height'] == 0 || $dimension['width'] == 0) { Kwf_Media_Output::output($output); } $cropX = 0; $cropY = 0; $cropHeight = $imageOriginal->getImageHeight(); $cropWidth = $imageOriginal->getImageWidth(); if ($imageOriginal->getImageHeight() / $dimension['height'] > $imageOriginal->getImageWidth() / $dimension['width']) { // orientate on width $cropHeight = $dimension['height'] * $imageOriginal->getImageWidth() / $dimension['width']; $cropY = ($imageOriginal->getImageHeight() - $cropHeight) / 2; } else { // orientate on height $cropWidth = $dimension['width'] * $imageOriginal->getImageHeight() / $dimension['height']; $cropX = ($imageOriginal->getImageWidth() - $cropWidth) / 2; } } else { //Calculate values relative to original image size $factor = Kwf_Media_Image::getHandyScaleFactor($fileRow->getFileSource()); $cropX = $this->_getParam('cropX') * $factor; $cropY = $this->_getParam('cropY') * $factor; $cropWidth = $this->_getParam('cropWidth') * $factor; $cropHeight = $this->_getParam('cropHeight') * $factor; } // Calculate values relative to preview image $image = new Imagick(); $image->readImageBlob($output['contents']); $previewFactor = 1; if ($image->getImageWidth() == $previewWidth) { $previewFactor = $image->getImageWidth() / $imageOriginal->getImageWidth(); } else { if ($image->getImageHeight() == $previewHeight) { $previewFactor = $image->getImageHeight() / $imageOriginal->getImageHeight(); } } $cropX = floor($cropX * $previewFactor); $cropY = floor($cropY * $previewFactor); $cropWidth = floor($cropWidth * $previewFactor); $cropHeight = floor($cropHeight * $previewFactor); $draw = new ImagickDraw(); if ($this->_isLightImage($output)) { $draw->setFillColor('black'); } else { $draw->setFillColor('white'); } $draw->setFillOpacity(0.3); // if cropX == 0 or cropY == 0 no rectangle should be drawn, because it // can't be 0 wide on topmost and leftmost position so it will result in // a 1px line which is wrong //Top region if ($cropY > 0) { $draw->rectangle(0, 0, $image->getImageWidth(), $cropY); } //Left region if ($cropX > 0) { if ($cropY > 0) { $draw->rectangle(0, $cropY + 1, $cropX, $cropY + $cropHeight - 1); } else { $draw->rectangle(0, $cropY, $cropX, $cropY + $cropHeight - 1); } } //Right region if ($cropY > 0) { $draw->rectangle($cropX + $cropWidth, $cropY + 1, $image->getImageWidth(), $cropY + $cropHeight - 1); } else { $draw->rectangle($cropX + $cropWidth, $cropY, $image->getImageWidth(), $cropY + $cropHeight - 1); } //Bottom region $draw->rectangle(0, $cropY + $cropHeight, $image->getImageWidth(), $image->getImageHeight()); $image->drawImage($draw); $output['contents'] = $image->getImageBlob(); Kwf_Media_Output::output($output); }