/** * Creates both a new version of the Image and a new thumbnail * according to its Imagetype * * Note that zero or NULL values are ignored. * @return boolean True on success, false otherwise. * @author Reto Kohli <*****@*****.**> */ function resize() { global $objDatabase; if (!$this->id) { //DBG::log("Image::resize(): No Image ID! Failed"); return false; } // Only applies to files that contain a // file name with a known extension if ($this->path == '' || !preg_match('/\\.(?:jpe?g|gif|png)$/i', $this->path)) { //DBG::log("Image::resize(): Invalid extension: $this->path"); return false; } // Get the thumbnail size for the associated type $arrInfo = \Cx\Core\ImageType\Controller\ImageType::getInfoArray($this->imagetype_key); //DBG::log("resize(): Info: ".var_export($arrInfo, true)); if ($arrInfo['width_thumb'] || $arrInfo['height_thumb']) { if (!self::scale($this->path, self::getThumbnailPath($this->path), true, $arrInfo['width_thumb'], $arrInfo['height_thumb'], $arrInfo['quality_thumb'])) { //DBG::log("Image::resize(): Failed to scale thumbnail"); return false; } } // Only resize the original if the width and/or height are limited if ($arrInfo['width'] || $arrInfo['height']) { $path = self::getJpegPath($this->path); if (!self::scale($this->path, $path, true, $arrInfo['width'], $arrInfo['height'], $arrInfo['quality'])) { //DBG::log("Image::resize(): Failed to scale myself"); return false; } // If the jpeg file name is different from the original name, // the original image is deleted to save disk space if ($this->path != $path) { File::delete_file($this->path); File::delete_file(self::getThumbnailPath($this->path)); } // The path *MUST* be updated $this->setPath($path); // And so *SHOULD* the size $size = getimagesize(ASCMS_DOCUMENT_ROOT . '/' . $path); if ($size[0] && $size[1]) { $this->setWidth($size[0]); $this->setHeight($size[1]); //DBG::log("Image::resize(): New size: {$this->getWidth()} x {$this->getHeight()}"); } //DBG::log("Image::resize(): returning result from store()"); return $this->store(); } //DBG::log("Image::resize(): No resizing, finished successfully"); return true; }
/** * Returns an image tag for the given Image object in the thumbnail * size specified by the corresponding Imagetype * * This adds alt, width, and height attributes with the values returned * by {@see Image::getPath()}, and {@see \Cx\Core\ImageType\Controller\ImageType::getInfoArrayWidth()} * methods repectively. * If the $attribute parameter contains one of the alt, width, or height * attributes (or corresponding style information), these will override * the data from the Image or Imagetype. * @param Image $objImage The Image object * @param string $attribute Additional optional attributes * @return string The HTML code for the image tag * @author Reto Kohli <*****@*****.**> */ static function getThumbnail($objImage, $attribute = '') { $type = $objImage->getImagetypeKey(); $objImagetype = \Cx\Core\ImageType\Controller\ImageType::getInfoArray($type); $width = $objImagetype['width_thumb']; $height = $objImagetype['height_thumb']; $path = $objImage->getPath(); return '<img src="' . contrexx_raw2encodedUrl($path) . '"' . (empty($width) || preg_match('/width[:=]/i', $attribute) ? '' : ' width="' . $width . '"') . (empty($height) || preg_match('/height[:=]/i', $attribute) ? '' : ' height="' . $height . '"') . (preg_match('/alt\\=/i', $attribute) ? '' : ' alt="' . $path . '"') . ($attribute ? ' ' . $attribute : '') . " />\n"; }