예제 #1
0
 /**
  * Helper function that returns scaled and croped images for media output
  *
  * Used by image components in getMediaOutput.
  * Tries to avoid scaling if not required (to keep gif animation intact)
  */
 public static function getMediaOutputForDimension($data, $dim, $type)
 {
     // calculate output width/height on base of getImageDimensions and given width
     $width = substr($type, strlen(Kwf_Media::DONT_HASH_TYPE_PREFIX));
     $width = substr($width, 0, strpos($width, '-'));
     if ($width) {
         $width = Kwf_Media_Image::getResponsiveWidthStep($width, Kwf_Media_Image::getResponsiveWidthSteps($dim, isset($data['image']) ? $data['image'] : $data['file']));
         $dim['height'] = $width / $dim['width'] * $dim['height'];
         $dim['width'] = $width;
     }
     $ret = array();
     if (isset($data['image'])) {
         $output = Kwf_Media_Image::scale($data['image'], $dim);
         $ret['contents'] = $output;
     } else {
         $sourceSize = @getimagesize($data['file']);
         $scalingNeeded = true;
         $resultingSize = Kwf_Media_Image::calculateScaleDimensions($data['file'], $dim);
         if ($sourceSize && array($resultingSize['crop']['width'], $resultingSize['crop']['height']) == array($sourceSize[0], $sourceSize[1]) && array($resultingSize['width'], $resultingSize['height']) == array($sourceSize[0], $sourceSize[1])) {
             $scalingNeeded = false;
         }
         if ($scalingNeeded) {
             //NOTE: don't pass actual size of the resulting image, scale() will calculate that on it's own
             //else size is calculated twice and we get rounding errors
             $uploadId = isset($data['uploadId']) ? $data['uploadId'] : null;
             $output = Kwf_Media_Image::scale($data['file'], $dim, $uploadId);
             $ret['contents'] = $output;
         } else {
             $ret['file'] = $data['file'];
         }
     }
     $ret['mimeType'] = $data['mimeType'];
     $ret['mtime'] = filemtime($data['file']);
     return $ret;
 }
예제 #2
0
 public function writeContent($setCoordinates = true)
 {
     $data = $this->_component->getImageData();
     if ($data && $data['file'] && is_file($data['file'])) {
         $source = $data['file'];
         $size = $this->getSize();
         $imageSize = array('cover' => false, 'width' => $this->_calculateMm($size['width']), 'height' => $this->_calculateMm($size['height']));
         $uploadId = isset($data['uploadId']) ? $data['uploadId'] : null;
         $content = Kwf_Media_Image::scale($source, $imageSize, $uploadId);
         $filter = new Kwf_Filter_Ascii();
         $tempFilename = tempnam('application/temp', 'pdfimage');
         file_put_contents($tempFilename, $content);
         $data = getimagesize($tempFilename);
         if ($data[2] == 2) {
             // nur jpgs ausgeben
             if ($setCoordinates && $this->addPageIfNecessary()) {
                 $x = $this->getX();
                 $this->SetY($this->getTopMargin());
                 $this->SetX($x);
             }
             $type = str_replace('image/', '', 'image/jpeg');
             $this->_pdf->Image($tempFilename, $this->getX(), $this->getY(), $this->_calculatePx($data[0]), $this->_calculatePx($data[1]), $type);
             if ($setCoordinates) {
                 $this->SetY($this->getY() + $size['height'] + 2);
             }
         }
         unlink($tempFilename);
         return true;
     }
     return false;
 }
예제 #3
0
 /**
  * Helper function that returns scaled and croped images for media output
  *
  * Used by image components in getMediaOutput.
  * Tries to avoid scaling if not required (to keep gif animation intact)
  */
 public static function getMediaOutputForDimension($data, $dim, $type)
 {
     if (isset($data['url'])) {
         $file = Kwf_Config::getValue('mediaCacheDir') . '/remotefile_' . md5($data['url']);
         if (!is_file($file)) {
             $httpClientConfig = array('timeout' => 20, 'persistent' => false);
             if (Kwf_Config::getValue('http.proxy.host')) {
                 $httpClientConfig['adapter'] = 'Zend_Http_Client_Adapter_Proxy';
                 $httpClientConfig['proxy_host'] = Kwf_Config::getValue('http.proxy.host');
                 $httpClientConfig['proxy_port'] = Kwf_Config::getValue('http.proxy.port');
             }
             $httpClient = new Zend_Http_Client($data['url'], $httpClientConfig);
             $request = $httpClient->request();
             if ($request->getStatus() == 200) {
                 file_put_contents($file, $request->getBody());
                 if (!getimagesize($file)) {
                     unlink($file);
                     throw new Kwf_Exception('File is no image: ' . $data['url']);
                 }
             } else {
                 throw new Kwf_Exception('Could not download file: ' . $data['url']);
             }
         }
         $data['file'] = $file;
     }
     // calculate output width/height on base of getImageDimensions and given width
     $width = substr($type, strlen(Kwf_Media::DONT_HASH_TYPE_PREFIX));
     $width = substr($width, 0, strpos($width, '-'));
     if ($width) {
         $width = Kwf_Media_Image::getResponsiveWidthStep($width, Kwf_Media_Image::getResponsiveWidthSteps($dim, isset($data['image']) ? $data['image'] : $data['file']));
         $dim['height'] = $width / $dim['width'] * $dim['height'];
         $dim['width'] = $width;
     }
     $ret = array();
     if (isset($data['image'])) {
         $output = Kwf_Media_Image::scale($data['image'], $dim);
         $ret['contents'] = $output;
     } else {
         $sourceSize = @getimagesize($data['file']);
         $scalingNeeded = true;
         $resultingSize = Kwf_Media_Image::calculateScaleDimensions($data['file'], $dim);
         if ($sourceSize && array($resultingSize['crop']['width'], $resultingSize['crop']['height']) == array($sourceSize[0], $sourceSize[1]) && array($resultingSize['width'], $resultingSize['height']) == array($sourceSize[0], $sourceSize[1])) {
             $scalingNeeded = false;
         }
         if ($scalingNeeded) {
             //NOTE: don't pass actual size of the resulting image, scale() will calculate that on it's own
             //else size is calculated twice and we get rounding errors
             $uploadId = isset($data['uploadId']) ? $data['uploadId'] : null;
             $output = Kwf_Media_Image::scale($data['file'], $dim, $uploadId);
             $ret['contents'] = $output;
         } else {
             $ret['file'] = $data['file'];
         }
     }
     $ret['mimeType'] = $data['mimeType'];
     $ret['mtime'] = filemtime($data['file']);
     return $ret;
 }
예제 #4
0
 public static function getMediaOutput($id, $type, $className)
 {
     $row = Kwf_Model_Abstract::getInstance($className)->getRow($id);
     $dim = self::getImageDimensions($type);
     if ($type == 'LoginImageLarge') {
         $type = 'LoginImage';
     }
     $uploadRow = $row->getParentRow($type);
     return array('contents' => Kwf_Media_Image::scale($uploadRow, $dim, $uploadRow->id), 'mimeType' => $row->getParentRow($type)->mime_type);
 }
예제 #5
0
 public static function getMediaOutput($uploadId, $type, $className)
 {
     $uploadRow = Kwf_Model_Abstract::getInstance('Kwf_Uploads_Model')->getRow($uploadId);
     if ($type == 'original') {
         return array('file' => $uploadRow->getFileSource(), 'mimeType' => $uploadRow->mime_type);
     } else {
         if ($type == 'preview') {
             $size = array('width' => 150, 'height' => 200, 'cover' => false);
             return array('contents' => Kwf_Media_Image::scale($uploadRow->getFileSource(), $size, $uploadRow->id), 'mimeType' => $uploadRow->mime_type);
         } else {
             throw new Kwf_Exception_NotFound();
         }
     }
 }
예제 #6
0
 public static function getMediaOutput($uploadId, $type, $className)
 {
     $uploadRow = Kwf_Model_Abstract::getInstance('Kwf_Uploads_Model')->getRow($uploadId);
     if (preg_match('#^preview-(\\d+)-(\\d+)$#', $type, $m)) {
         $size = array('width' => $m[1], 'height' => $m[2], 'cover' => false);
     } else {
         if ($type == 'hover') {
             $size = array('width' => 250, 'height' => 250, 'cover' => false);
         } else {
             throw new Kwf_Exception_NotFound();
         }
     }
     return array('contents' => Kwf_Media_Image::scale($uploadRow->getFileSource(), $size, $uploadRow->id), 'mimeType' => $uploadRow->mime_type);
 }
예제 #7
0
 public function testHtml()
 {
     $html = $this->_root->getComponentById(2100)->render();
     $doc = new DOMDocument();
     $doc->strictErrorChecking = FALSE;
     $doc->loadHTML($html);
     $xml = simplexml_import_dom($doc);
     $img = $xml->xpath("//img");
     $this->assertEquals(3, count($img));
     $this->assertEquals(100, (string) $img[0]['width']);
     $this->assertEquals(100, (string) $img[0]['height']);
     $src = (string) $img[1]['src'];
     $this->assertTrue(!!preg_match('#/media/([^/]+)/([^/]+)/([^/]+)#', (string) $img[0]['src'], $m));
     $o = call_user_func(array($m[1], 'getMediaOutput'), $m[2], $m[3], $m[1]);
     $this->assertEquals('image/png', $o['mimeType']);
     $im = new Imagick($o['file']);
     $this->assertEquals(16, $im->getImageWidth());
     $this->assertEquals(16, $im->getImageHeight());
     $this->assertEquals(Kwf_Media_Image::scale(Kwf_Model_Abstract::getInstance('Kwc_Composite_Images_Image_UploadsModel')->getRow(1)->getFileSource(), array(16, 16, 'cover' => true)), file_get_contents($o['file']));
 }
예제 #8
0
 private function _assert($componentId, $smallImage, $largeImage)
 {
     $html = $this->_root->getComponentById($componentId)->render();
     $doc = new DOMDocument();
     $doc->strictErrorChecking = FALSE;
     $doc->loadHTML($html);
     $xml = simplexml_import_dom($doc);
     $img = $xml->xpath("//img");
     $this->assertEquals(1, count($img));
     $this->assertEquals($smallImage['width'], (string) $img[0]['width']);
     $this->assertEquals($smallImage['height'], (string) $img[0]['height']);
     $this->assertTrue(!!preg_match('#^/media/([^/]+)/([^/]+)/([^/]+)#', (string) $img[0]['src'], $m));
     $o = Kwf_Media::getOutput($m[1], $m[2], $m[3]);
     $this->assertEquals($smallImage['mimeType'], $o['mimeType']);
     $im = new Imagick();
     $im->readImageBlob($o['contents']);
     $this->assertEquals($smallImage['width'], $im->getImageWidth());
     $this->assertEquals($smallImage['height'], $im->getImageHeight());
     $this->assertEquals(Kwf_Media_Image::scale(Kwf_Model_Abstract::getInstance('Kwc_Basic_ImageEnlarge_UploadsModel')->getRow($smallImage['uploadId'])->getFileSource(), array('width' => $smallImage['width'], 'height' => $smallImage['height'], 'cover' => true), $smallImage['uploadId']), $o['contents']);
     $a = $xml->xpath("//a");
     $this->assertEquals(1, count($a));
     $this->assertEquals('{"width":' . $largeImage['width'] . ',"height":' . $largeImage['height'] . ',"style":"CenterBox","adaptHeight":true,"lightboxUrl":"\\/foo1\\/image"}', (string) $a[0]['data-kwc-lightbox']);
     $this->assertContains('/media/Kwc_Basic_ImageEnlarge_EnlargeTagWithoutSmall_TestComponent/1800-linkTag', (string) $a[0]['href']);
     $html = $this->_root->getComponentById($componentId . '-linkTag_imagePage')->render();
     $doc = new DOMDocument();
     $doc->strictErrorChecking = FALSE;
     $doc->loadHTML($html);
     $xml = simplexml_import_dom($doc);
     $img = $xml->xpath("//img");
     $this->assertEquals(1, count($img));
     $this->assertEquals($largeImage['width'], (string) $img[0]['width']);
     $this->assertEquals($largeImage['height'], (string) $img[0]['height']);
     $this->assertTrue(!!preg_match('#^/media/([^/]+)/([^/]+)/([^/]+)#', (string) $img[0]['src'], $m));
     $o = Kwf_Media::getOutput($m[1], $m[2], $m[3]);
     $this->assertEquals($largeImage['mimeType'], $o['mimeType']);
     $im = new Imagick();
     $im->readImage($o['file']);
     $this->assertEquals($largeImage['width'], $im->getImageWidth());
     $this->assertEquals($largeImage['height'], $im->getImageHeight());
     $this->assertEquals(Kwf_Media_Image::scale(Kwf_Model_Abstract::getInstance('Kwc_Basic_ImageEnlarge_UploadsModel')->getRow($largeImage['uploadId'])->getFileSource(), array($largeImage['width'], $largeImage['height'], 'cover' => true)), file_get_contents($o['file']));
 }
 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);
 }
예제 #10
0
 public function testWithSmallImageUploadedHtml()
 {
     $html = $this->_root->getComponentById(1802)->render();
     $doc = new DOMDocument();
     $doc->strictErrorChecking = FALSE;
     $doc->loadHTML($html);
     $xml = simplexml_import_dom($doc);
     $img = $xml->xpath("//img");
     $this->assertEquals(1, count($img));
     $this->assertEquals(10, (string) $img[0]['width']);
     $this->assertEquals(10, (string) $img[0]['height']);
     $this->assertTrue(!!preg_match('#^/media/([^/]+)/([^/]+)/([^/]+)#', (string) $img[0]['src'], $m));
     $o = call_user_func(array($m[1], 'getMediaOutput'), $m[2], $m[3], $m[1]);
     $this->assertEquals('image/gif', $o['mimeType']);
     $im = new Imagick();
     if (isset($o['contents'])) {
         $contents = $o['contents'];
     } else {
         $contents = file_get_contents($o['file']);
     }
     $im->readImageBlob($contents);
     $this->assertEquals(10, $im->getImageWidth());
     $this->assertEquals(10, $im->getImageHeight());
     $this->assertEquals(Kwf_Media_Image::scale(Kwf_Model_Abstract::getInstance('Kwc_Basic_ImageEnlarge_UploadsModel')->getRow('2')->getFileSource(), array(10, 10, 'cover' => true), 2), $contents);
     $a = $xml->xpath("//a");
     $this->assertEquals(1, count($a));
     $this->assertEquals('{"width":210,"height":70,"style":"CenterBox","adaptHeight":true,"lightboxUrl":"\\/foo3\\/image"}', (string) $a[0]['data-kwc-lightbox']);
     $this->assertContains('/media/Kwc_Basic_ImageEnlarge_EnlargeTag_TestComponent/1802-linkTag/', (string) $a[0]['href']);
     $html = $this->_root->getComponentById('1802-linkTag_imagePage')->render();
     $doc = new DOMDocument();
     $doc->strictErrorChecking = FALSE;
     $doc->loadHTML($html);
     $xml = simplexml_import_dom($doc);
     $img = $xml->xpath("//img");
     $this->assertEquals(1, count($img));
     $this->assertTrue(!!preg_match('#^/media/([^/]+)/([^/]+)/([^/]+)#', (string) $img[0]['src'], $m));
     $o = call_user_func(array($m[1], 'getMediaOutput'), $m[2], $m[3], $m[1]);
     $this->assertEquals('image/gif', $o['mimeType']);
     $im = new Imagick();
     if (isset($o['contents'])) {
         $contents = $o['contents'];
     } else {
         $contents = file_get_contents($o['file']);
     }
     $im->readImageBlob($contents);
     // crop is set, because show_selection is set and else compare will fail
     $content = Kwf_Media_Image::scale(Kwf_Model_Abstract::getInstance('Kwc_Basic_ImageEnlarge_UploadsModel')->getRow('2')->getFileSource(), array(210, 70, 'cover' => false, 'crop' => array('height' => 70, 'width' => 210, 'x' => 0, 'y' => 0)));
     $image = new Imagick();
     $image->readimageblob($content);
     $this->assertEquals(210, $im->getImageWidth());
     $this->assertEquals(70, $im->getImageHeight());
     $this->assertEquals($content, $contents);
 }
예제 #11
0
 private function _testScale($size, $expectedSize = null)
 {
     if (!$expectedSize) {
         $expectedSize = $size;
     }
     $i = Kwf_Media_Image::scale(KWF_PATH . '/images/information.png', $size);
     $im = new Imagick();
     $im->readImageBlob($i);
     $this->assertEquals($expectedSize[0], $im->getImageWidth());
     $this->assertEquals($expectedSize[1], $im->getImageHeight());
 }
 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);
 }