/ Published in: PHP
Can't seem to remember where I found this and it's no longer in my bookmarks. If you know where this came from please provide the link so credit can go to the right person.
Expand |
Embed | Plain Text
<?php function resize_image($img, $thumb_width, $newfilename) { $max_width=$thumb_width; //Check if GD extension is loaded return false; } //Get Image size info switch ($image_type) { case 1: $im = imagecreatefromgif($img); break; case 2: $im = imagecreatefromjpeg($img); break; case 3: $im = imagecreatefrompng($img); break; } /*** calculate the aspect ratio ***/ $aspect_ratio = (float) $height_orig / $width_orig; /*** calulate the thumbnail width based on the height ***/ $thumb_width = $max_width; //while($thumb_height>$max_width) { // $thumb_width-=10; // $thumb_height = round($thumb_width * $aspect_ratio); // } $newImg = imagecreatetruecolor($thumb_width, $thumb_height); /* Check if this image is PNG or GIF, then set if Transparent*/ if(($image_type == 1) || ($image_type==3)) { imagealphablending($newImg, false); imagesavealpha($newImg,true); $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127); imagefilledrectangle($newImg, 0, 0, $thumb_width, $thumb_height, $transparent); } imagecopyresampled($newImg, $im, 0, 0, 0, 0, $thumb_width, $thumb_height, $width_orig, $height_orig); //Generate the file, and rename it to $newfilenamea switch ($image_type) { case 1: imagegif($newImg,$newfilename); break; case 2: imagejpeg($newImg,$newfilename); break; case 3: imagepng($newImg,$newfilename); break; } return $newfilename; } ?>
You need to login to post a comment.
