Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class ImageResize {
- public function img_resize($src, $dst, $width, $height, $crop=0){
- $gis = getimagesize($src);
- $w = $gis[0]; $h = $gis[1];
- $type = $gis[2];
- switch($type){
- case '6': $img = imagecreatefromwbmp($src); break;
- case '1': $img = imagecreatefromgif($src); break;
- case '2': $img = imagecreatefromjpeg($src); break;
- case '3': $img = imagecreatefrompng($src); break;
- default : return "Unsupported picture type!";
- }
- // resize
- if($crop){
- if($w < $width or $h < $height) $width = $w; $height = $h;
- $ratio = max($width/$w, $height/$h);
- $h = $height / $ratio;
- $x = ($w - $width / $ratio) / 2;
- $w = $width / $ratio;
- }
- else{
- if($w < $width and $h < $height) $width = $w; $height = $h;
- $ratio = min($width/$w, $height/$h);
- $width = $w * $ratio;
- $height = $h * $ratio;
- $x = 0;
- }
- $new = imagecreatetruecolor($width, $height);
- // preserve transparency
- if($type == 1 or $type == 3){
- 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 '6': imagewbmp($new, $dst); break;
- case '1': imagegif($new, $dst); break;
- case '2': imagejpeg($new, $dst); break;
- case '3': imagepng($new, $dst); break;
- }
- return true;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement