3
I have this class in PHP that when running resizes the images contained in a folder. If you observe, the resizing is triggered by these instructions, one for each desired size.
resize_and_crop('banner.jpg', 'banner_100x100.jpg', 100, 100);
resize_and_crop('banner.jpg', 'banner_200x100.jpg', 200, 100);
resize_and_crop('banner.jpg', 'banner_200x300.jpg', 200, 300);
The complete function is in the Pastebin and it perfectly resizes in 3 copies the original image, each of a different size as determined in the use of the function resize_and_crop()
. I’ll put the file to run inside the images folder, so you don’t need paths.
Note that the function
awaits 4 parameters:
- current image name
- new image name
- width ()
- height (height)
Question: how to make a loop that returns all images from that folder and inside this one loop
apply the function resize_and_crop()
for each of the returned images?
// RESIZE AN IMAGE PROPORTIONALLY AND CROP TO THE CENTER
function resize_and_crop($original_image_url, $thumb_image_url, $thumb_w, $thumb_h, $quality = 100)
{
// ACQUIRE THE ORIGINAL IMAGE: http://php.net/manual/en/function.imagecreatefromjpeg.php
$original = imagecreatefromjpeg($original_image_url);
if (!$original) return FALSE;
// GET ORIGINAL IMAGE DIMENSIONS
list($original_w, $original_h) = getimagesize($original_image_url);
// RESIZE IMAGE AND PRESERVE PROPORTIONS
$thumb_w_resize = $thumb_w;
$thumb_h_resize = $thumb_h;
if ($original_w > $original_h) ...
This is just a piece of Function that appears complete on the external link.
Because I am receiving this error: http://puu.sh/luW4K/3f707f1170.png
– Marcos Vinicius
Looks like there’s some file
.jpg
that is not an imagejpg
– Jeferson Assis