// Load the image $img = imagecreatefromjpeg('image.jpg'); // Get the current dimensions $width = imagesx($img); $height = imagesy($img); // Set the new dimensions $newWidth = 500; $newHeight = round($newWidth * $height / $width); // Create a new blank image $newImg = imagecreatetruecolor($newWidth, $newHeight); // Resize the original image and copy it onto the new image imagecopyresized($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); // Output the new image to the browser header('Content-Type: image/jpeg'); imagejpeg($newImg); imagedestroy($newImg); imagedestroy($img);
// Load the image $img = new Imagick('image.jpg'); // Get the current dimensions $width = $img->getImageWidth(); $height = $img->getImageHeight(); // Calculate new dimensions $newWidth = 500; $newHeight = round($newWidth * $height / $width); // Crop the image $img->cropImage($newWidth, $newHeight, 0, 0); // Output the new image to the browser header('Content-Type: image/jpeg'); echo $img; $img->destroy();
// Load the image $img = Image::make('image.jpg'); // Resize the image $img->resize(500, null, function ($constraint) { $constraint->aspectRatio(); }); // Output the new image to the browser header('Content-Type: image/jpeg'); echo $img->response('jpg'); $img->destroy();In conclusion, the most suitable package and library can be determined based on the specific needs and requirements of the project. GD is a basic library suitable for simple image manipulation, while Imagick offers more advanced features. Intervention Image is a popular choice for its simpler and more intuitive API.