Efficient Jpeg Image Resizing in PHP

Efficient JPEG Image Resizing in PHP

People say that ImageMagick is much faster. At best just compare both libraries and measure that.

  1. Prepare 1000 typical images.
  2. Write two scripts -- one for GD, one
    for ImageMagick.
  3. Run both of them a few times.
  4. Compare results (total execution
    time, CPU and I/O usage, result
    image quality).

Something which the best everyone else, could not be the best for you.

Also, in my opinion, ImageMagick has much better API interface.

Resize image to a higher resolution with PHP

resize.php

<?php

/*
* File: SimpleImage.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/

class SimpleImage {

var $image;
var $image_type;

function load($filename) {

$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {

$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {

$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {

$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {

if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {

imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {

imagepng($this->image,$filename);
}
if( $permissions != null) {

chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {

if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {

imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {

imagepng($this->image);
}
}
function getWidth() {

return imagesx($this->image);
}
function getheight() {

return imagesy($this->image);
}
function resizeToheight($height) {

$ratio = $height / $this->getheight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}

function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}

function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}

function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getheight());
$this->image = $new_image;
}

}
?>

Output.php

<?
include ("rezise.php");
$piclocation="images/123.jpg";
$picsavelocation="images/123.jpg";

$image = new SimpleImage();
$image->load(''.$piclocation.'');
$image->resize(188,282);
$image->save(''.$piclocation.'');


header('Content-type: image/jpeg');
echo file_get_contents(''.$picsavelocation.'');
unlink($save_rand_name);
?>

ImageMagick, most efficient way to resize images?

$img->setImageCompressionQuality(0) sets the quality of the image. If you don't want to loose any quality set it to 100, but best for optimisation between 60-80.

Resizing an image on website for efficiency.

The best way to do this is to use some sort of image re-factoring service.

I have written my own one that uses ffmpeg and imagemagik to resize images on the fly and to generate arbitrarily sized thumbnails from videos. I memcache the results to make subsequent requests super snappy, and have some interesting additions such as automatic Point of Interest detection using Face Detection and Image Entropy, with the aim being "nice thumbnails, no matter the size"

An example of such a service is src.sencha.io - the documentation for this service is here but I have included the important bits below.

Specify Image Size

<img src='http://src.sencha.io/320/200/http://yourdomain.com/path/to/image.jpg'
alt='My constrained image'
width='320'
height='200' />

This will take your image (http://yourdomain.com/path/to/image.jpg) and run in through the resizing service, returning a 320x200 version of your image. You cannot set the gravity/point-of-interest using this service though (as far as I can tell).


You can also use this service to change formats, resize dataurls, do percentage resizes and use the resizing service to detect the width/height of the user agent requesting the image.

There are many such services available on the web.

What's the best (fastest for performance) code in PHP to resize very large images (3000x6000px)?

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.

Resizing images without PHP GD Library

The Imagine PHP library sf the easiest I've worked with - it can use GD, Imagick, or GMagick to resize/crop images.

<?php
$imagine = new Imagine\Gd\Imagine();
$size = new Imagine\Image\Box(40, 40);
$imagine->open('/path/to/large_image.jpg')
->thumbnail($size, Imagine\Image\ImageInterface::THUMBNAIL_INSET)
->save('/path/to/thumbnail.png');


Related Topics



Leave a reply



Submit