1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
<?php
namespace Rain\Tpl\Plugin;
require_once __DIR__ . '/../Plugin.php';
class ImageResize extends \Rain\Tpl\Plugin{
protected $hooks = array('beforeParse');
protected $quality = 80;
protected $crop = TRUE;
public function beforeParse(\ArrayAccess $context){
// set variables
$html = $context->code;
$template_basedir = $context->template_basedir;
$quality = $this->quality;
$auto_crop = $this->crop;
$conf = $context->conf;
$img_cache_dir = $template_basedir = $conf['cache_dir'];
// get the template base directory
$template_directory = $conf['base_url'] . $conf['tpl_dir'] . $template_basedir;
// reduce the path
$path = preg_replace('/\w+\/\.\.\//', '', $template_directory );
$exp = $sub = array();
$image_resized = false;
// match the images
if( preg_match_all( '/<img((?:\s*(src="(?<src>.*?)"))|(\s*(width="(?<width>.*?)"))|(\s*height="(?<height>.*?)")|(\s*resize="(?<resize>.*?)")|(\s*crop="(?<crop>.*?)"))*.*?>/', $html, $matches ) ){
for( $i=0,$n=count($matches[0]); $i<$n; $i++ ){
$tag = $matches[0][$i];
$src = $matches['src'][$i];
$w = $matches['width'][$i];
$h = $matches['height'][$i];
$resize = $matches['resize'][$i];
if( $auto_crop )
$crop = $matches['crop'][$i] == 'false' ? false : true;
else
$crop = $matches['crop'][$i] == 'true' ? true : false;
if( $w > 0 && $h > 0 && $resize != 'false' ){
echo $src;exit;
$new_tag = preg_replace( '/(.*?)src="(.*?)"(.*?)/', '$1src="<?php echo Rain\Tpl\Plugin\ImageResize::imgResize(\''.$src.'\', \''.$img_cache_dir.'\', \''.$w.'\', \''.$h.'\', \''.$quality.'\', \''.$crop.'\' ); ?>"$3', $tag );
$html = str_replace( $tag, $new_tag, $html );
$image_resized = true;
}
}
if( $image_resized )
$html = '<?php require_once(__FILE__); ?>' . $html;
}
$context->code = $html;
}
public function setQuality($quality) {
$this->quality = (int) $quality;
return $this;
}
public function setCrop($crop) {
$this->crop = (string) $crop;
return $this;
}
public static function imgResize( $src, $dest, $w, $h, $quality, $crop ){
$ext = substr(strrchr($src, '.'),1);
$dest = $dest . 'img.'. md5( $src . $crop . $quality ) . $w . 'x' . $h . '.' . $ext;
if( !file_exists( $dest ) )
static::rainImgResize( $src, $dest, $w, $h, $quality, $crop );
return $dest;
}
public static function rainImgResize($src, $dst, $width, $height, $quality, $crop=0){
if(!list($w, $h) = getimagesize($src)) return "Unsupported picture type!";
$type = strtolower(substr(strrchr($src,"."),1));
if($type == 'jpeg') $type = 'jpg';
switch($type){
case 'bmp': $img = imagecreatefromwbmp($src); break;
case 'gif': $img = imagecreatefromgif($src); break;
case 'jpg': $img = imagecreatefromjpeg($src); break;
case 'png': $img = imagecreatefrompng($src); break;
default : return "Unsupported picture type!";
}
// resize
if($crop){
if($w < $width or $h < $height) return "Picture is too small!";
$ratio = max($width/$w, $height/$h);
$h = $height / $ratio;
$x = ($w - $width / $ratio) / 2;
$w = $width / $ratio;
}
else{
if($w < $width and $h < $height) return "Picture is too small!";
$ratio = min($width/$w, $height/$h);
$width = $w * $ratio;
$height = $h * $ratio;
$x = 0;
}
$new = imagecreatetruecolor($width, $height);
// preserve transparency
if($type == "gif" or $type == "png"){
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
imagealphablending($new, false);
imagesavealpha($new, true);
}
imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);
switch($type){
case 'bmp': imagewbmp($new, $dst, $quality); break;
case 'gif': imagegif($new, $dst, $quality); break;
case 'jpg': imagejpeg($new, $dst, $quality); break;
case 'png': imagepng($new, $dst, $quality); break;
}
return true;
}
}
|