ACC SHELL
<?php
/**
* class for resizing images
*
* @author Heralecky
*/
class Image {
public $image;
public $image_type;
public static $img_prefix = "/x/files/"; // src not contains "/" will connect with prefix
public static $cache_src = "/x/img_cache/"; // directory of cache available from browser
public static $app_preprefix = "."; // application path from your index.php to $img_prefix and $cache_src paths
public static $no_source_img_src = "/views/_default/www/img/no_source_img.jpg"; // error
public static $placeholder_img_src = "/views/_default/www/img/placeholder_img.png"; // expected placeholder
public static $file_img = "/views/_default/www/img/file.png";
public function __construct(){ }
/**
* returns tag of resized image
*
* @param string $src Same as in src of img tag. Example:"/www/files/obrazek.jpg" or simply obrazek.jpg
* empty return image placeholder
*
* @param string $param
* - width
* - height
* - format [crop|max|min|white]
* - return_url [true|false] if returns only url
* - enlarge [true|false] enlarge if the pucture is smaller? Default: true
* - lightbox [true|false]
* - over ... pixels around which will be throw out, useable only for crop format jet
* - whatever ... will display like "<img ... whatever="value" ... />
*
* @return string
*/
public function _($src="", $params=array()) {
if(strpos($src,"/")===false && $src){
$src = self::$img_prefix . $src; // for shorten syntax
}
$width = 0;
$height = 0;
$return_url = false;
$enlarge = true;
$over = 0;
$style = "";
$title_attr = "";
$rel_attr = "";
$format = false;
$lightbox = false;
$f_exists; // just speeding_up variable
if(empty($params['alt'])) $params['alt'] = "";
foreach ($params as $index => $value) { // handle parameters
switch ($index) {
case "format":
case "f": $format = $value;
break;
case "lightbox":
case "lb": $lightbox = !empty($value);
break;
case "enlarge": $enlarge = !empty($value);
break;
case "over": $over = $value;
break;
case "over": $return_url = !empty($value);
break;
case "w":
case "width": $width = $value;
break;
case "h":
case "height": $height = $value;
break;
case "title": if($index="title") $title_attr = ' title="'.$value.'"';
case "rel": if($index="rel") $rel_attr = ' rel="'.$value.'"';
default: if($value) $style .= $index . '= "' . str_replace('"','\\"',$value) . '" ';
}
}
$file_icon = true;
foreach (array(".gif",".png",".jpg") as $ext) if(stripos($src,$ext) || !$src) $file_icon = false;
$cached = $this->cache_name($src,$width,$height,$format,$enlarge,$over,$file_icon);
$src_param = $src;
if($src==""){ // empty $src returns placeholder
$path = self::$placeholder_img_src;
$html_comment = 'param $src is empty';
$lightbox = false;
}elseif($this->file_exists($cached)){ // take image from cache
$path = self::$cache_src . $cached;
}elseif(!($f_exists=@file_exists(self::$app_preprefix . $src)) && @getimagesize($src)===false){ // oops... image not exist
$path = self::$no_source_img_src;
$html_comment = 'image "' . $src . '" not exist';
$lightbox = false;
}elseif(!$width && !$height){ // no parameters, no fun, don't cry
$path = $src;
$html_comment = 'no size defined';
}else{ // create new cache
if($file_icon){
$src = self::$file_img;
$format = "white";
}
if($f_exists){
$this->load(self::$app_preprefix . $src); //local image
}else{
$this->load($src); //image from external server
}
if($width && $height){
switch ($format) {
case "min":
$this->resize_min($width, $height, $enlarge);
break;
case "max":
$this->resize_max($width, $height, $enlarge);
break;
case "white":
$this->resize_max($width, $height, $enlarge);
$this->crop($width, $height);
break;
case "crop": // crop is also default
default:
$this->resize_min($width+2*$over, $height+2*$over, $enlarge);
if($enlarge || $width<$this->getWidth() || $height<$this->getHeight()){
$this->crop($width, $height);
}
}
}else{ // if width or height is not defined
if($enlarge || $width<$this->getWidth() || $height<$this->getHeight()){
$this->resize($width,$height);
}
}
$this->save(self::$app_preprefix . self::$cache_src . $cached);
$path = self::$cache_src . $cached;
}
if($format=="min" || $format=="max" || !$width || !$height){ // this formats can change passed width or height
$width = 0; $height = 0;
// RECOUNT UNDER IS TOO SLOW
// load image with app_prefix if src need it and get proportions
/*$this->load((strpos($path,self::$img_prefix)===0||strpos($path,self::$cache_src)===0?self::$app_preprefix:"") . $path);
$width = $this->getWidth();
$height = $this->getHeight();*/
}
$tag = '<img src="' . $path . '" ';
if($width) $tag .= 'width="'.$width.'" ';
if($height) $tag .= 'height="'.$height.'" ';
$tag .= $style . ' />';
if($lightbox) $tag = '<a href="'. $src_param . '"' . $title_attr . $rel_attr . '>' . $tag . '</a>';
if(!empty($html_comment)) $tag .= '<!-- '. $html_comment .' -->';
if($return_url){
return $path;
}else{
return $tag;
}
}
/**
* Parse html content and replace images by cached. Images width "no-parse" class will be ignored
*
* @param string $cont
* @return string
*/
public function parse($cont){
$t = str_replace("\n","",$cont);
$parsed = $cont;
// go throw every image in $cont
while(1){
$parse = true;
$img = preg_replace("/.*(<img[^>]*>).*/i","$1",$t);
// if this is not inline image, parse it
if(!strpos($img,"data:image")){
if($img == $t) break;
if(strpos($img,"no-parse")) $parse = false;
$src = urldecode(preg_replace("/.*src *= *[\"|\']([^\"\']+).*/i","$1",$img)); if($src==$img) $src=false;
$style = preg_replace("/.*style *= *[\"|\']([^\"\']+).*/i","$1",$img); if($style==$img) $style=false;
$width = preg_replace("/.*width *= *[\"|\']([^\"\']+).*/i","$1",$img); if($width==$img) $width=false;
$height = preg_replace("/.*height *= *[\"|\']([^\"\']+).*/i","$1",$img); if($height==$img) $height=false;
$align = preg_replace("/.*align *= *[\"|\']([^\"\']+).*/i","$1",$img); if($align==$img) $align=false;
$hspace = preg_replace("/.*hspace *= *[\"|\']([^\"\']+).*/i","$1",$img); if($hspace==$img) $hspace=false;
$vspace = preg_replace("/.*vspace *= *[\"|\']([^\"\']+).*/i","$1",$img); if($vspace==$img) $vspace=false;
if(($width || $height) && $parse){
if($src==$img){Tools::critical("Image::parse() error while parsing part"); return $cont;}
if($width==$img) $width=0;
if($height==$img) $height=0;
$lb = false;
if(!preg_match("/.*".str_replace(array("/","\"","'"),array("\\/","\\\"","\\'"),$img)."[^(<a)]*<\/a>.*/i", $cont)) $lb = true;
$parsed = str_replace($img,$this->_($src,array("width"=>$width,"height"=>$height,"lb"=>$lb, 'style'=>$style, 'align'=>$align, 'hspace'=>$hspace, 'vspace'=>$vspace)),$parsed);
}
}
$t = str_replace($img,"",$t);
}
return $parsed;
}
/**
* Nacte obrazek do $this->image
*
* @filename - adresa kde je ulozeny obrazek
*/
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if ($this->image_type == IMAGETYPE_JPEG) {
$this->image = imagecreatefromjpeg($filename);
} elseif ($this->image_type == IMAGETYPE_GIF) {
$this->image = imagecreatefromgif($filename);
} elseif ($this->image_type == IMAGETYPE_PNG) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if ($image_type == IMAGETYPE_JPEG) {
$return = imagejpeg($this->image, $filename, $compression);
} elseif ($image_type == IMAGETYPE_GIF) {
$return = imagegif($this->image, $filename);
} elseif ($image_type == IMAGETYPE_PNG) {
$return = imagepng($this->image, $filename);
}
if ($permissions != null) {
chmod($filename, $permissions);
}
return $return;
}
function output($image_type=IMAGETYPE_JPEG) {
if ($image_type == IMAGETYPE_JPEG) {
imagejpeg($this->image);
} elseif ($image_type == IMAGETYPE_GIF) {
imagegif($this->image);
} elseif ($image_type == IMAGETYPE_PNG) {
imagepng($this->image);
}
}
function resize_max($width, $height, $enlarge=true) { // zmenší do rámečku
if ($enlarge || ($this->getHeight() > $height || $this->getWidth() > $width)) {
if ($width / $height > $this->getWidth() / $this->getHeight())
$this->resize(0,$height);
else
$this->resize($width);
}
}
// zmenší tak aby obě strany měli alespoň zadanou délku (jedna bude nejspíš přesahovat)
function resize_min($width, $height, $enlarge=true) { // vyplní
if ($enlarge || ($this->getHeight() > $height && $this->getWidth() > $width)) {
if ($width / $height < $this->getWidth() / $this->getHeight())
$this->resize(0,$height);
else
$this->resize($width);
}
}
// vyřízne uprostřed obrázku tento rozměr
function crop($width, $height, $trim = false) {
if ($trim) {
if ($width > $this->getWidth())
$width = $this->getWidth();
if ($height > $this->getHeight())
$height = $this->getHeight();
}
$new_image = imagecreatetruecolor($width, $height);
imagecopy($new_image, $this->image, 0, 0, ($this->getWidth() - $width) / 2, ($this->getHeight() - $height) / 2, $width, $height);
if($width > $this->getWidth() || $height > $this->getHeight()){
//die($width . "xxxx" . $this->getWidth());
$color = imagecolorexact($new_image, 255, 255, 255);
imagefill($new_image, 0, 0, $color);
imagefill($new_image, $width - 1, $height - 1, $color);
}
$this->image = $new_image;
}
// zmenší procentuálně
function scale($scale) {
$width = $this->getWidth() * $scale / 100;
$height = $this->getheight() * $scale / 100;
$this->resize($width, $height);
}
function resize($width=0, $height=0) {
if($width==0 && $height==0) return; //error
if($width==0){
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
}
if($height==0){
$ratio = $width / $this->getWidth();
$height = $this->getHeight() * $ratio;
}
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
////////// PRIVATE ////////////
private function cache_name($src,$width,$height,$format,$enlarge,$over,$file=false){
$format_shortcuts = array(0=>"","crop"=>"","max"=>"m","min"=>"n","white"=>"w");
$spec = $format_shortcuts[$format] . ($enlarge?"":"s") . ($over?"o".$over:""); // shortcuts of operations
$name = basename($src);
$namein = str_ireplace(array(".gif",".png",".jpg"),"",$name);
if($file){ $name = "file.png"; $namein = "file"; }
return str_replace($namein,$namein . "_" . $width . "x" . $height . $spec,$name);
}
//////// FUNCTIONS READY TO CONNECT WITH DB TO SPEED UP //////////
//////// because file_exists() is slow //////////
// this function can be faster, if you connect it with db
private function file_exists($cached_name){
return file_exists(self::$app_preprefix . self::$cache_src . $cached_name);
// mysql_result("select id from ...");
}
private function save_cache($cached_name){
// save it to db
}
public function clear_cache(){
// clear db and folder
}
}
?>
ACC SHELL 2018