php简单缩略图类|image.class.php_PHP教程

WBOY
Release: 2016-07-20 11:16:50
Original
948 people have browsed it

使用方法:


$img = new iamge;
$img->resize('dstimg.jpg', 'srcimg.jpg', 300, 400);
说明:这个是按照比例缩放,dstimg.jpg是目标文件,srcimg.jpg是源文件,后面的是目标文件的宽和高
$img->thumb('dstimg.jpg', 'scrimg.jpg', 300, 300);
说明:这个是按照比例缩略图,比如常用在头像缩略图的时候,dstimg.jpg是目标文件,srcimg.jpg是源文件,后面的是目标文件的宽和高
这个是针对GD库才这样麻烦的,如果采用Imagick的话,就只需要两个函数就实现上面的功能,去查下文档就找到了。

class image{

public function resize($dstImg, $srcImg, $dstW, $dstH){
list($srcW, $srcH) = getimagesize($srcImg);
$scale = min($dstW/$srcW, $dstH/$srcH);
$newW = round($srcW * $scale);
$newH = round($srcH * $scale);
$newImg = imagecreatetruecolor($newW, $newH);
$srcImg = imagecreatefromjpeg($srcImg);
imagecopyresampled($newImg, $srcImg, 0, 0, 0, 0, $newW, $newH, $srcW, $srcH);
imagejpeg($newImg, $dstImg);
}

public function thumb($dstImg, $srcImg, $dstW, $dstH){
list($srcW, $srcH) = getimagesize($srcImg);
$scale = max($dstW/$srcW, $dstH/$srcH);
$newW = round($dstW/$scale);
$newH = round($dstH/$scale);
$x = ($srcW - $newW)/2;
$y = ($srcH - $newH)/2;
$newImg = imagecreatetruecolor($dstW, $dstH);
$srcImg = imagecreatefromjpeg($srcImg);
imagecopyresampled($newImg, $srcImg, 0, 0, $x, $y, $dstW, $dstH, $newW, $newH);
imagejpeg($newImg, $dstImg);
}

}

function createFromType($type, $srcImg){
$function = "imagecreatefrom$type";
return $function($srcImg);
}
//为了解决不同图片格式的问题

www.bkjia.com true http://www.bkjia.com/PHPjc/372434.html TechArticle 使用方法: $img = new iamge; $img-resize('dstimg.jpg', 'srcimg.jpg', 300, 400); 说明:这个是按照比例缩放,dstimg.jpg是目标文件,srcimg.jpg是源文件,后面...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!