Home > Backend Development > PHP Tutorial > php simple thumbnail class|image.class.php_PHP tutorial

php simple thumbnail class|image.class.php_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-20 11:16:50
Original
1051 people have browsed it

How to use:


$img = new iamge;
$img->resize('dstimg.jpg', 'srcimg.jpg', 300, 400);
Note: This is scaled according to the proportion. dstimg.jpg is the target file, srcimg.jpg is the source file, and the following is the width and height of the target file
$img->thumb('dstimg.jpg', 'scrimg.jpg', 300, 300);
Note: This is a proportional thumbnail. For example, it is commonly used in avatar thumbnails. dstimg.jpg is the target file, srcimg.jpg is the source file, and the following is the width and height of the target file
This is only troublesome for the GD library. If you use Imagick, you only need two functions to implement the above functions. You can find it by checking the documentation.

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);
}
//In order to solve the problem of different image formats

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/372434.htmlTechArticleUsage: $img = new iamge; $img-resize('dstimg.jpg', 'srcimg.jpg ', 300, 400); Note: This is scaled according to the proportion. dstimg.jpg is the target file, srcimg.jpg is the source file, and the following...
Related labels:
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 Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template