- <?php
- 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);
- }
- //为了解决不同图片格式的问题
- //该片段来自于http://www.codesnippet.cn/detail/271120137527.html
来源: http://www.codesnippet.cn/detail/271120137527.html