Create thumb (Resize and crop image) image from the image using PHP GD lib.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
<?php // function resizeCrop($source_file, $folde_path, $max_width, $max_height = '0') { $temp_image_name = explode("/", $source_file); $dst_dir = $folde_path . "/" . end($temp_image_name); $quality = 100; if ($max_height == '0') { $max_height = $max_width; } $imgsize = getimagesize($source_file); $width = $imgsize[0]; $height = $imgsize[1]; $mime = $imgsize['mime']; switch ($mime) { case 'image/gif': $image_create = "imagecreatefromgif"; $image = "imagegif"; break; case 'image/png': $image_create = "imagecreatefrompng"; $image = "imagepng"; $quality = 7; break; case 'image/jpeg': $image_create = "imagecreatefromjpeg"; $image = "imagejpeg"; $quality = 80; break; default: return false; break; } $dst_img = imagecreatetruecolor($max_width, $max_height); /////////////// imagealphablending($dst_img, false); imagesavealpha($dst_img, true); $transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127); imagefilledrectangle($dst_img, 0, 0, $max_width, $max_height, $transparent); ///////////// $src_img = $image_create($source_file); $width_new = $height * $max_width / $max_height; $height_new = $width * $max_height / $max_width; //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa if ($width_new > $width) { //cut point by height $h_point = (($height - $height_new) / 2); //copy image imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new); } else { //cut point by width $w_point = (($width - $width_new) / 2); imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height); } $image($dst_img, $dst_dir, $quality); if ($dst_img) imagedestroy($dst_img); if ($src_img) imagedestroy($src_img); } $folder_path = "dfolder"; $source_file = "source.png"; $max_width = "280"; $max_height = "210"; resizeCrop($source_file, $folder_path, $max_width, $max_height); ?> |
More Stories
CPU & Memory usage in PHP
Install PHP mcrypt extension on Ubuntu
Text to speech