Wasn’t easy to find a working solution, so I’m posting here as a reminder.
This is a PHP function to generate images thumbnails that handles: png, jpg, gif and also transparency
I’m not the author of this function, I’ve just mixed two solutions together, and fixed some small issues.
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
<?php function createThumb($src, $dest, $desired_width = false, $desired_height = false) { /* If no dimenstion for thumbnail given, return false */ if (!$desired_height && !$desired_width) return false; $fparts = pathinfo($src); $ext = strtolower($fparts['extension']); /* if its not an image return false */ if (!in_array($ext, array( 'gif', 'jpg', 'png', 'jpeg' ))) return false; /* read the source image */ if ($ext == 'gif') $resource = imagecreatefromgif($src); else if ($ext == 'png') $resource = imagecreatefrompng($src); else if ($ext == 'jpg' || $ext == 'jpeg') $resource = imagecreatefromjpeg($src); $width = imagesx($resource); $height = imagesy($resource); /* find the “desired height” or “desired width” of this thumbnail, relative * to each other, if one of them is not given */ if (!$desired_height) $desired_height = floor($height * ($desired_width / $width)); if (!$desired_width) $desired_width = floor($width * ($desired_height / $height)); /* create a new, “virtual” image */ $virtual_image = imagecreatetruecolor($desired_width, $desired_height); switch ($ext) { case "png": // integer representation of the color black (rgb: 0,0,0) $background = imagecolorallocate($virtual_image, 0, 0, 0); // removing the black from the placeholder imagecolortransparent($virtual_image, $background); // turning off alpha blending (to ensure alpha channel information // is preserved, rather than removed (blending with the rest of the // image in the form of black)) imagealphablending($virtual_image, false); // turning on alpha channel information saving (to ensure the full range // of transparency is preserved) imagesavealpha($virtual_image, true); break; case "gif": // integer representation of the color black (rgb: 0,0,0) $background = imagecolorallocate($virtual_image, 0, 0, 0); // removing the black from the placeholder imagecolortransparent($virtual_image, $background); break; } /* copy source image at a resized size */ imagecopyresampled($virtual_image, $resource, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height); /* create the physical thumbnail image to its destination */ /* Use correct function based on the desired image type from $dest thumbnail * source */ $fparts = pathinfo($dest); $ext = strtolower($fparts['extension']); /* if dest is not an image type, default to jpg */ if (!in_array($ext, array( 'gif', 'jpg', 'png', 'jpeg' ))) $ext = 'jpg'; $dest = $fparts['dirname'] . '/' . $fparts['filename'] . '.' . $ext; if ($ext == 'gif') imagegif($virtual_image, $dest); else if ($ext == 'png') imagepng($virtual_image, $dest, 1); else if ($ext == 'jpg' || $ext == 'jpeg') imagejpeg($virtual_image, $dest, 100); return array( 'width' => $width, 'height' => $height, 'new_width' => $desired_width, 'new_height' => $desired_height, 'dest' => $dest ); } |
Sources :
Leave a Reply