public function __toString()
 {
     if ($this->image) {
         return $this->image->__toString();
     }
     return "";
 }
Exemple #2
0
 /**
  * @static
  * @param Asset_Image_Thumbnail_Config $config
  * @return string
  */
 public static function process(Asset_Image $asset, Asset_Image_Thumbnail_Config $config)
 {
     $format = strtolower($config->getFormat());
     // simple detection for source type if SOURCE is selected
     if ($format == "source") {
         $typeMapping = array("gif" => "gif", "jpeg" => "jpeg", "jpg" => "jpeg", "png" => "png");
         $fileExt = Pimcore_File::getFileExtension($asset->getFilename());
         if ($typeMapping[$fileExt]) {
             $format = $typeMapping[$fileExt];
         } else {
             // use PNG if source doesn't have a valid mapping
             $format = "png";
         }
     }
     $filename = "thumb_" . $asset->getId() . "__" . $config->getName() . "." . $format;
     $fsPath = PIMCORE_TEMPORARY_DIRECTORY . "/" . $filename;
     $path = str_replace(PIMCORE_DOCUMENT_ROOT, "", $fsPath);
     // check for existing and still valid thumbnail
     if (is_file($fsPath) and filemtime($fsPath) > $asset->getModificationDate()) {
         return $path;
     }
     // transform image
     $image = Asset_Image::getImageTransformInstance();
     if (!$image->load($asset->getFileSystemPath())) {
         return "/pimcore/static/img/image-not-supported.png";
     }
     $transformations = $config->getItems();
     if (is_array($transformations) && count($transformations) > 0) {
         foreach ($transformations as $transformation) {
             if (!empty($transformation)) {
                 $arguments = array();
                 $mapping = self::$argumentMapping[$transformation["method"]];
                 if (is_array($transformation["arguments"])) {
                     foreach ($transformation["arguments"] as $key => $value) {
                         $position = array_search($key, $mapping);
                         if ($position !== false) {
                             $arguments[$position] = $value;
                         }
                     }
                 }
                 ksort($arguments);
                 if (count($mapping) == count($arguments)) {
                     call_user_func_array(array($image, $transformation["method"]), $arguments);
                 } else {
                     $message = "Image Transform failed: cannot call method `" . $transformation["method"] . "´ with arguments `" . implode(",", $arguments) . "´ because there are too few arguments";
                     Logger::error($message);
                 }
             }
         }
     }
     $image->save($fsPath, $format, $config->getQuality());
     return $path;
 }
 public function getImageThumbnailAction()
 {
     $image = Asset_Image::getById(intval($this->_getParam("id")));
     $thumbnail = null;
     if ($this->_getParam("thumbnail")) {
         $thumbnail = $image->getThumbnailConfig($this->_getParam("thumbnail"));
     }
     if (!$thumbnail) {
         if ($this->_getParam("config")) {
             $thumbnail = $image->getThumbnailConfig(Zend_Json::decode($this->_getParam("config")));
         } else {
             $thumbnail = $image->getThumbnailConfig($this->_getAllParams());
         }
     }
     $format = strtolower($thumbnail->getFormat());
     if ($format == "source") {
         $thumbnail->setFormat("PNG");
         $format = "png";
     }
     if ($this->_getParam("cropPercent")) {
         $thumbnail->addItemAt(0, "cropPercent", array("width" => $this->_getParam("cropWidth"), "height" => $this->_getParam("cropHeight"), "y" => $this->_getParam("cropTop"), "x" => $this->_getParam("cropLeft")));
         $hash = md5(Pimcore_Tool_Serialize::serialize($this->_getAllParams()));
         $thumbnail->setName("auto_" . $hash);
     }
     $this->getResponse()->setHeader("Content-Type", "image/" . $format, true);
     if ($this->_getParam("download")) {
         $this->getResponse()->setHeader("Content-Disposition", 'attachment; filename="' . $image->getFilename() . '"');
     }
     $thumbnailFile = PIMCORE_DOCUMENT_ROOT . $image->getThumbnail($thumbnail);
     $imageContent = file_get_contents($thumbnailFile);
     $fileExtension = Pimcore_File::getFileExtension($thumbnailFile);
     if (in_array($fileExtension, array("gif", "jpeg", "jpeg", "png"))) {
         header("Content-Type: image/" . $fileExtension);
     } else {
         header("Content-Type: " . $image->getMimetype());
     }
     header("Content-Length: " . filesize($thumbnailFile));
     echo $imageContent;
     exit;
 }
Exemple #4
0
 /**
  * @see Object_Class_Data::getVersionPreview
  * @param Asset_Image $data
  * @return string
  */
 public function getVersionPreview($data)
 {
     if ($data instanceof Asset_Image) {
         return '<img src="/admin/asset/get-image-thumbnail/id/' . $data->getId() . '/width/100/height/100/aspectratio/true" />';
     }
 }
 /**
  * create a random asset image with no properties set
  * @return Asset
  */
 protected function createRandomAssetImage()
 {
     $asset = Asset_Image::create(1, array("filename" => uniqid() . rand(10, 99) . ".jpg", "data" => file_get_contents(TESTS_PATH . "/resources/assets/images/image" . rand(1, 4) . ".jpg"), "userOwner" => 1));
     return $asset;
 }
 public function testCopyAndDeleteAsset()
 {
     $assetList = new Asset_List();
     $assetList->setCondition("`filename` like '%_data%' and `type` = 'folder'");
     $assets = $assetList->load();
     $parent = $assets[0];
     $this->assertTrue($parent instanceof Asset_Folder);
     //remove childs if there are some
     if ($parent->hasChilds()) {
         foreach ($parent->getChilds() as $child) {
             $child->delete();
         }
     }
     $assetList = new Asset_List();
     $assetList->setCondition("`filename` like '%_data%' and `type` != 'folder'");
     $assets = $assetList->load();
     $image = $assets[0];
     $this->assertTrue($image instanceof Asset_Image);
     $this->assertFalse($parent->hasChilds());
     $service = new Asset_Service(User::getById(1));
     //copy as child
     $service->copyAsChild($parent, $parent);
     $this->assertTrue($parent->hasChilds());
     $this->assertTrue(count($parent->getChilds()) == 1);
     $childs = $parent->getChilds();
     $this->assertTrue(Test_Tool::assetsAreEqual($parent, $childs[0], true));
     //copy as child no. 2
     $service->copyAsChild($parent, $image);
     $this->assertTrue($parent->hasChilds());
     $this->assertTrue(count($parent->getChilds()) == 2);
     //copy recursivley
     $rootNode = Asset::getById(1);
     $copy = $service->copyRecursive($rootNode, $parent);
     $this->assertTrue($copy->hasChilds());
     $this->assertTrue(count($copy->getChilds()) == 2);
     $this->assertTrue(Test_Tool::assetsAreEqual($parent, $copy, true));
     //create unequal assets
     $asset1 = Asset_Image::create(1, array("filename" => uniqid() . rand(10, 99) . ".jpg", "data" => file_get_contents(TESTS_PATH . "/resources/assets/images/image1" . ".jpg"), "userOwner" => 1));
     $asset2 = Asset_Image::create(1, array("filename" => uniqid() . rand(10, 99) . ".jpg", "data" => file_get_contents(TESTS_PATH . "/resources/assets/images/image2" . ".jpg"), "userOwner" => 1));
     $this->assertFalse(Test_Tool::assetsAreEqual($asset1, $asset2, true));
     //copy contents
     $asset1 = $service->copyContents($asset1, $asset2);
     $this->assertTrue(Test_Tool::assetsAreEqual($asset1, $asset2, true));
     //todo copy contents must fail if types differ
     //delete recusively
     $shouldBeDeleted[] = $copy->getId();
     $childs = $copy->getChilds();
     foreach ($childs as $child) {
         $shouldBeDeleted[] = $child->getId();
     }
     $copy->delete();
     foreach ($shouldBeDeleted as $id) {
         $o = Asset::getById($id);
         $this->assertFalse($o instanceof Asset);
     }
 }
Exemple #7
0
 /**
  * Receives a Webservice_Data_Document_Element from webservice import and fill the current tag's data
  *
  * @abstract
  * @param  Webservice_Data_Document_Element $data
  * @return void
  */
 public function getFromWebserviceImport($wsElement)
 {
     $data = $wsElement->value;
     if ($data->id !== null) {
         $this->alt = $data->alt;
         $this->id = $data->id;
         if (is_numeric($this->id)) {
             $this->image = Asset_Image::getById($this->id);
             if (!$this->image instanceof Asset_Image) {
                 throw new Exception("cannot get values from web service import - referenced image with id [ " . $this->id . " ] is unknown");
             }
         } else {
             throw new Exception("cannot get values from web service import - id is not valid");
         }
     }
 }
 public function getImageThumbnailAction()
 {
     $image = Asset_Image::getById(intval($this->_getParam("id")));
     $thumbnail = null;
     if ($this->_getParam("thumbnail")) {
         $thumbnail = $image->getThumbnailConfig($this->_getParam("thumbnail"));
     }
     if (!$thumbnail) {
         if ($this->_getParam("config")) {
             $thumbnail = $image->getThumbnailConfig(Zend_Json::decode($this->_getParam("config")));
         } else {
             $thumbnail = $image->getThumbnailConfig($this->_getAllParams());
         }
     }
     $format = strtolower($thumbnail->getFormat());
     if ($format == "source") {
         $thumbnail->setFormat("PNG");
         $format = "png";
     }
     if ($this->_getParam("cropPercent")) {
         $thumbnail->addItemAt(0, "cropPercent", array("width" => $this->_getParam("cropWidth"), "height" => $this->_getParam("cropHeight"), "y" => $this->_getParam("cropTop"), "x" => $this->_getParam("cropLeft")));
         $hash = md5(Pimcore_Tool_Serialize::serialize($this->_getAllParams()));
         $thumbnail->setName("auto_" . $hash);
     }
     $this->getResponse()->setHeader("Content-Type", "image/" . $format, true);
     if ($this->_getParam("download")) {
         $this->getResponse()->setHeader("Content-Disposition", 'attachment; filename="' . $image->getFilename() . '"');
     }
     readfile(PIMCORE_DOCUMENT_ROOT . $image->getThumbnail($thumbnail));
     $this->removeViewRenderer();
 }