I found many examples on how to resize an image however I’m curious about what’s the best (fastest) code in PHP to do it and for very large images (over 1000px).
I wrote this simple example… anyone knows a better implementation?
<?php
$filename = 'myimage.jpg';
$image = imagecreatefromjpeg($filename);
$scale = 50; // resize the image to 50% of its original width and height
$width = imagesx($image);
$width_scaled = $width * $scale/100;
$height = imagesy($image);
$height_scaled = $height * $scale/100;
$image_scaled = imagecreatetruecolor($width_scaled, $height_scaled);
imagecopyresampled($image_scaled, $image, 0, 0, 0, 0, $width_scaled, $height_scaled, $width, $height);
?>
I’d use imagecopyresized. I think it’s faster because you skip one step. However, the actual resizing is probably the most time consuming, so you won’t gain much unless you find a separate library/plugin that is faster.