Source of file ExtendedImage.php
Size: 18,494 Bytes - Last Modified: 2022-02-21T10:01:18+00:00
/var/www/docs.ssmods.com/process/src/code/extensions/ExtendedImage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 | <?php /** * extended-image/code/ExtendedImage.php * * Can Return the Image as Base64 String * Can set Backgroundcolor on padded resize * */ class ExtendedImage extends DataExtension { /** * @config * @var integer */ private static $get_jpeg_default_quality = 75; /** * @config * @var integer */ private static $get_jpeg_default_background_color = "FFFFFF"; /** * Return an XHTML img tag for this Image. * @return string */ public function getBase64Tag() { if($this->owner->exists()) { $url = $this->owner->getBase64Source(); $title = ($this->owner->Title) ? $this->owner->Title : $this->owner->Filename; if($this->owner->Title) { $title = Convert::raw2att($this->owner->Title); } else { if(preg_match("/([^\/]*)\.[a-zA-Z0-9]{1,6}$/", $title, $matches)) $title = Convert::raw2att($matches[1]); } return "<img src=\"$url\" alt=\"$title\" />"; } } /** * retrun the Base64 Notation of the Image */ public function getBase64Source(){ $Fullpath = $this->owner->getFullPath(); $cache = SS_Cache::factory('Base64Image'); $cachekey = md5($Fullpath); if(!($Base64Image = $cache->load($cachekey))){ $Base64Image = base64_encode(file_get_contents($Fullpath)); $cache->save($Base64Image); } $type = strtolower($this->owner->getExtension()); return "data:image/".$type.";base64,".$Base64Image; } /** * Detect a Face inside the image * * returns null or an Array with x, y coordinates and w = width/height (square) * @return Array */ public function DetectFace(){ $detector = new svay\FaceDetector(); $detector->faceDetect($this->owner->getFullPath()); return $detector->getFace(); } /** * returns the image with the face marked in a red square * * @return Image_Cached|SecureImage_Cached */ public function DetectedFace(){ if($this->owner->exists()) { $cacheFile = $this->owner->cacheDetectedFaceFilename(); if(!file_exists(Director::baseFolder()."/".$cacheFile) || isset($_GET['flush'])) { $this->owner->generateDetectedFaceImage(); } if($this->owner instanceof SecureImage) $cached = new SecureImage_Cached($cacheFile); else $cached = new Image_Cached($cacheFile); // Pass through the title so the templates can use it $cached->Title = $this->owner->Title; $cached->ID = $this->owner->ID; $cached->ParentID = $this->owner->ParentID; return $cached; } } /** * Return the filename for the cached image. * @return string */ public function cacheDetectedFaceFilename() { $folder = $this->owner->ParentID ? $this->owner->Parent()->Filename : ASSETS_DIR . "/"; $format = 'DetectedFace'; if($this->owner instanceof SecureImage){ $file = pathinfo($this->owner->Name); return $folder . "_resampled/".md5($format."-".$file['filename']).".".$file['extension']; }else{ return $folder . "_resampled/".$format."-".$this->owner->Name; } } /** * Generate an image on the specified format. It will save the image * at the location specified by cacheFilename(). The image will be generated * using the specific 'generate' method for the specified format. */ public function generateDetectedFaceImage() { $cacheFile = $this->owner->cacheDetectedFaceFilename(); $backend = Injector::inst()->createWithArgs(Image::get_backend(), array( Director::baseFolder()."/" . $this->owner->Filename )); if($backend->hasImageResource()){ $backend = $backend->detectedFaceImage($this->DetectFace()); if($backend){ $backend->writeTo(Director::baseFolder()."/" . $cacheFile); } } } /** * Return an XHTML img tag for this Image, * or NULL if the image file doesn't exist on the filesystem. * * @return string */ public function TagWithClass($cssclass) { if($this->owner->exists()) { $url = $this->owner->getURL(); $title = ($this->owner->Title) ? $this->owner->Title : $this->owner->Filename; if($this->owner->Title) { $title = Convert::raw2att($this->owner->Title); } else { if(preg_match("/([^\/]*)\.[a-zA-Z0-9]{1,6}$/", $title, $matches)) { $title = Convert::raw2att($matches[1]); } } return "<img src=\"$url\" alt=\"$title\" class=\"$cssclass\" />"; } } /** * Merge the Image onto anotherone fitting it with a min padding * @param type $backgroundimage * @param type $padding */ public function MergeOver($backgroundimage, $padding = 10) { return $this->owner->getMergedImage($format = 'over', $padding, $backgroundimage); } /** * use the current Image as a background for the merging image * @param type $overlayimage * @param type $padding */ public function MergeUnder($overlayimage, $padding = 10) { return $this->owner->getMergedImage($format = 'under', $padding, $overlayimage); } /** * Return an image object representing the merged image. * @param type $format * @param type $padding * @param type $mergeimage * @return Image_Cached */ public function getMergedImage($format, $padding, $mergeimage) { if($this->owner->exists() && Director::fileExists($mergeimage)) { $cacheFile = $this->owner->cacheMergedFilename($format, $padding, $mergeimage); if(!file_exists(Director::baseFolder()."/".$cacheFile) || isset($_GET['flush'])) { // merge the current image over the given Merging Image if($format == 'over') $this->owner->generateMergedImage($padding, $mergeimage, $this->owner->getFullPath(), $cacheFile); // merge the current image over the given Merging Image else $this->owner->generateMergedImage($padding, $this->owner->getFullPath(), $mergeimage, $cacheFile); } if($this->owner instanceof SecureImage) $cached = new SecureImage_Cached($cacheFile); else $cached = new Image_Cached($cacheFile); // Pass through the title so the templates can use it $cached->Title = $this->owner->Title; $cached->ID = $this->owner->ID; $cached->ParentID = $this->owner->ParentID; return $cached; } } /** * Return the filename for the cached image, given it's format name and arguments. * @param string $format The format name. * @param string $padding * @param string $mergedimage (<- path, md5() this) * @return string */ public function cacheMergedFilename($format, $padding, $mergedimage) { $folder = $this->owner->ParentID ? $this->owner->Parent()->Filename : ASSETS_DIR . "/"; $format = 'merged-'.$format.$padding.md5($mergedimage); // Ermitteln, ob eine der Dateien ein png ist $image_pathinfo = pathinfo($this->owner->Filename); $merge_pathinfo = pathinfo($mergedimage); if($this->owner instanceof SecureImage){ $file = pathinfo($this->owner->Name); if(strtolower($image_pathinfo['extension']) == 'png' || strtolower($merge_pathinfo['extension']) == 'png'){ $mergedName = $folder . "_resampled/".md5($format."-".$file['filename']).".".$file['extension']; $mergedName = pathinfo($mergedName); return $mergedName['dirname'].'/'.$mergedName['filename'].'_'.$image_pathinfo['extension'].'.png'; }else{ return $folder . "_resampled/".md5($format."-".$file['filename']).".".$file['extension']; } }else{ if(strtolower($image_pathinfo['extension']) == 'png' || strtolower($merge_pathinfo['extension']) == 'png'){ $mergedName = $folder . "_resampled/$format-" . $this->owner->Name; $mergedName = pathinfo($mergedName); return $mergedName['dirname'].'/'.$mergedName['filename'].'_'.$image_pathinfo['extension'].'.png'; }else{ return $folder . "_resampled/$format-" . $this->owner->Name; } } } /** * genereate the merged image */ public function generateMergedImage($padding, $bgImagePath, $overlayImagePath, $cacheFile){ $bgImage = Injector::inst()->createWithArgs(Image::get_backend(), array( $bgImagePath )); $ovImage = Injector::inst()->createWithArgs(Image::get_backend(), array( $overlayImagePath )); $frontImage = $this->owner->generateFit($ovImage, ($bgImage->getWidth() - (2*$padding)), ($bgImage->getHeight() - (2*$padding))); $backend = $bgImage->merge($frontImage); if($backend){ $backend->writeTo(Director::baseFolder()."/" . $cacheFile); } } /** * Resize this Image by both width and height with transparent Background, using padded resize. Use in templates with $transparentPad. * @param type $width * @param type $height */ public function TransparentPad($width, $height){ return (($this->owner->getWidth() == $width) && ($this->owner->getHeight() == $height)) ? $this->owner : $this->owner->getTransparentFormattedImage('TransparentPad', $width, $height); } /** * Return an image object representing the image in the given format. * This image will be generated using generateFormattedImage(). * The generated image is cached, to flush the cache append ?flush=1 to your URL. * @param string $format The name of the format. * @param string $arg1 An argument to pass to the generate function. * @param string $arg2 A second argument to pass to the generate function. * @return Image_Cached */ public function getTransparentFormattedImage($format, $arg1 = null, $arg2 = null) { if($this->owner->exists()) { $cacheFile = $this->owner->cacheTransparentFilename($format, $arg1, $arg2); if(!file_exists(Director::baseFolder()."/".$cacheFile) || isset($_GET['flush'])) { $this->owner->generateTransparentFormattedImage($format, $arg1, $arg2); } if($this->owner instanceof SecureImage) $cached = new SecureImage_Cached($cacheFile); else $cached = new Image_Cached($cacheFile); // Pass through the title so the templates can use it $cached->Title = $this->owner->Title; $cached->ID = $this->owner->ID; $cached->ParentID = $this->owner->ParentID; return $cached; } } /** * Return the filename for the cached image, given it's format name and arguments. * @param string $format The format name. * @param string $arg1 The first argument passed to the generate function. * @param string $arg2 The second argument passed to the generate function. * @return string */ public function cacheTransparentFilename($format, $arg1 = null, $arg2 = null) { $folder = $this->owner->ParentID ? $this->owner->Parent()->Filename : ASSETS_DIR . "/"; $format = $format.$arg1.$arg2; if($this->owner instanceof SecureImage){ $file = pathinfo($this->owner->Name); return $folder . "_resampled/".md5($format."-".$file['filename']).".".$file['extension']; }else{ return $folder . "_resampled/".$format."-".$this->owner->Name; } } /** * Generate an image on the specified format. It will save the image * at the location specified by cacheFilename(). The image will be generated * using the specific 'generate' method for the specified format. * @param string $format Name of the format to generate. * @param string $arg1 Argument to pass to the generate method. * @param string $arg2 A second argument to pass to the generate method. */ public function generateTransparentFormattedImage($format, $arg1 = null, $arg2 = null) { $cacheFile = $this->owner->cacheTransparentFilename($format, $arg1, $arg2); $backend = Injector::inst()->createWithArgs(Image::get_backend(), array( Director::baseFolder()."/" . $this->owner->Filename )); if($backend->hasImageResource()){ $generateFunc = "generate$format"; if($this->owner->hasMethod($generateFunc)){ $backend = $this->owner->$generateFunc($backend, $arg1, $arg2); if($backend){ $backend->writeTo(Director::baseFolder()."/" . $cacheFile); } } else { USER_ERROR("Image::generateTransparentFormattedImage - Image $format function not found.",E_USER_WARNING); } } } /** * Resize this Image by both width and height, using padded resize. Use in templates with $SetSize. * @return GD */ public function generateTransparentPad(Image_Backend $backend, $width, $height) { if(!$backend){ user_error("Image::generateTransparentFormattedImage - generateTransparentPad is being called by legacy code" . " or Image::\$backend is not set.",E_USER_WARNING); }else{ return $backend->transparentPaddedResize($width, $height); } } /** * Blur the Image */ public function Blur($intensity = 'normal'){ return $this->owner->getBluredImage($intensity); } /** * Return an image object representing the blured image. * @return Image_Cached */ public function getBluredImage($intensity){ if($this->owner->exists()) { $cacheFile = $this->owner->cacheBluredFilename($intensity); if(!file_exists(Director::baseFolder()."/".$cacheFile) || isset($_GET['flush'])) { // blur the current image $this->owner->generateBluredImage($intensity); } if($this->owner instanceof SecureImage) $cached = new SecureImage_Cached($cacheFile); else $cached = new Image_Cached($cacheFile); // Pass through the title so the templates can use it $cached->Title = $this->owner->Title; $cached->ID = $this->owner->ID; $cached->ParentID = $this->owner->ParentID; return $cached; } } /** * Return the filename for the cached image. * @return string */ public function cacheBluredFilename($intensity) { $folder = $this->owner->ParentID ? $this->owner->Parent()->Filename : ASSETS_DIR . "/"; $format = 'blured-'.$intensity; if($this->owner instanceof SecureImage){ $file = pathinfo($this->owner->Name); return $folder . "_resampled/".md5($format."-".$file['filename']).".".$file['extension']; }else{ return $folder . "_resampled/".$format."-".$this->owner->Name; } } /** * Generate an image on the specified format. It will save the image * at the location specified by cacheFilename(). The image will be generated * using the specific 'generate' method for the specified format. */ public function generateBluredImage($intensity){ $cacheFile = $this->owner->cacheBluredFilename($intensity); $backend = Injector::inst()->createWithArgs(Image::get_backend(), array( Director::baseFolder()."/" . $this->owner->Filename )); $backend = $backend->blur($intensity); if($backend){ $backend->writeTo(Director::baseFolder()."/" . $cacheFile); } } /** * Generate a jpeg image from the source * * set quality and backgroundColor for Transparency * * @param $quality * @param $backgroundColor */ public function ToJPEG($quality = null, $backgroundColor = null) { if (!$quality) $quality = Config::inst()->get('ExtendedImage', 'get_jpeg_default_quality'); if (!$backgroundColor) $backgroundColor = Config::inst()->get('ExtendedImage', 'get_jpeg_default_background_color'); return $this->owner->getJPEGImage($quality, $backgroundColor); } /** * Return an image object representing the blured image. * @return Image_Cached */ public function getJPEGImage($quality, $backgroundColor){ if($this->owner->exists()) { $pathinfo = pathinfo($this->owner->Name); $cacheFile = $this->owner->cacheToJPEGFilename($quality, $backgroundColor, $pathinfo); list($width, $height, $type, $attr) = getimagesize($this->owner->getFullPath()); if($type == IMAGETYPE_JPEG && $quality == 100) return $this->owner; if(!file_exists(Director::baseFolder()."/".$cacheFile) || isset($_GET['flush'])) { // convert the current file to jpeg $this->owner->generateToJPEGImage($quality, $backgroundColor, $pathinfo, $type); } if($this->owner instanceof SecureImage) $cached = new SecureImage_Cached($cacheFile); else $cached = new Image_Cached($cacheFile); // Pass through the title so the templates can use it $cached->Title = $this->owner->Title; $cached->ID = $this->owner->ID; $cached->ParentID = $this->owner->ParentID; return $cached; } } /** * Return the filename for the cached image. * @return string */ public function cacheToJPEGFilename($quality, $backgroundColor, $pathinfo) { $folder = $this->owner->ParentID ? $this->owner->Parent()->Filename : ASSETS_DIR . "/"; $format = $pathinfo['extension'].'tojpeg-'.$quality.'-'.$backgroundColor; if($this->owner instanceof SecureImage){ return $folder . "_resampled/".md5($format."-".$pathinfo['filename']).".jpg"; }else{ return $folder . "_resampled/".$format."-".$pathinfo['filename'].".jpg"; } } /** * Generate an image on the specified format. It will save the image * at the location specified by cacheFilename(). The image will be generated * using the specific 'generate' method for the specified format. */ public function generateToJPEGImage($quality, $backgroundColor, $pathinfo, $type){ $cacheFile = $this->owner->cacheToJPEGFilename($quality, $backgroundColor, $pathinfo); $backend = Injector::inst()->createWithArgs(Image::get_backend(), array( Director::baseFolder()."/" . $this->owner->Filename )); $backend = $backend->toJpeg($backgroundColor, $type); if($backend){ $backend->setQuality($quality); $backend->writeTo(Director::baseFolder()."/" . $cacheFile); } } } |