Server Side Image Resizer

The code uses PHP to resize an image (currently only jpeg). Using this method, the resized image is of much better quality than a browser-side resizing. The file size of the new downsized image is also smaller (quicker to download).

The code comes in two parts:

  • imageResizer() is used to process the image
  • loadimage() inserts the image url in a simpler format

   function imageResizer($url, $width, $height) {

		header('Content-type: image/jpeg');

		list($width_orig, $height_orig) = getimagesize($url);

		$ratio_orig = $width_orig/$height_orig;

		if ($width/$height > $ratio_orig) {
		  $width = $height*$ratio_orig;
		} else {
		  $height = $width/$ratio_orig;

		// This resamples the image
		$image_p = imagecreatetruecolor($width, $height);
		$image = imagecreatefromjpeg($url);
		imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

		// Output the image
		imagejpeg($image_p, null, 100);

	//works with both POST and GET
	$method = $_SERVER['REQUEST_METHOD'];
	if ($method == 'GET') {

		imageResize($_GET['url'], $_GET['w'], $_GET['h']);
	 } elseif ($method == 'POST') {

	    imageResize($_POST['url'], $_POST['w'], $_POST['h']);

	// makes the process simpler
	function loadImage($url, $width, $height){
         echo 'image.php?url=', urlencode($url) ,



Above code would be in a file called image.php.

Images would be displayed like this:

<img src="<?php loadImage('image.jpg', 50, 50) ?>"


  1. User Avatar
    Permalink to comment#

    YAY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1 im going nuts my snippets made it u dont how happy i am :)

    Check out my site

  2. User Avatar
    Permalink to comment#

    on thing chris u should show who made the snippets

  3. User Avatar
    Peter Dubrovski
    Permalink to comment#

    Whats about GIF and PNG?

    $method = $_SERVER[‘REQUEST_METHOD’];

    Should go with

    imageResize($_REQUEST[‘url’], $_REQUEST[‘w’], $_REQUEST[‘h’]);

  4. User Avatar
    Permalink to comment#

    Hi! How would you format the link so that it includes the required alternate tag for the image?

  5. User Avatar
    Tom Gulik
    Permalink to comment#

    I am trying to link to images in specific directories but it encodes URL so images come back black.
    Is there a way to modify script to prevent encoding URL?

    • User Avatar
      Paul Garcia
      Permalink to comment#

      I think (not sure) your problem is not ENCODEDING stuff.
      I think problems is URL invocation:
      Depends of PHP installation your URLs has to be /root/ directory based not domain (http://www…..) related, means:
      if you try to use a image in a directory “inside” same php installation (means image is in the same server as the resize script) you have to use something like this:
      /root/www/domain/htpdocs/images/yourimage.jpg instead:
      to find out your “real” path use: PHP supervariable

      $myPath = $_SERVER[“DOCUMENT_ROOT”];
      $myImage = $mypath . “/images/myimagen.jpg”;

      or better check this trick (that works perfect for us in all or scripts):

      if (!file_exists($urlImage)) $urlImage=str_ireplace(‘http://’.$_SERVER[“HTTP_HOST”],$_SERVER[“DOCUMENT_ROOT”],$urlImage);

      Got it? it reemplace “” with your “real” path IF “don’t” find the image (means internal image)

      Hope this help you.

  6. User Avatar
    Paul Garcia
    Permalink to comment#

    Be aware of the high CPU RESOURCES used by functions like imagecreatetruecolor(), imagecreatefromjpeg(), imagecopyresampled() and other PHP image’s related functions.
    We used a PHP resize approcach in a high traffic website and we have to remove it because CPU SERVER RESOURCES were going crazy a couple of times a crash occurs (a resize process for every single user) we opted for a different approach (a WordPress like) at the moment of upload we create different sizes to be served (WordPress do that when a image is used in their posts)

  7. User Avatar
    Andrea Verlicchi
    Permalink to comment#

    This would work perfectly with a client-side service like picturePolyfill. Give it a try!

  8. User Avatar
    Permalink to comment#

    How do you add support for png?

  9. User Avatar
    Permalink to comment#

    This resize code is very good for version 1.0.

    For version 2.0, it would be great to save the aspect-ratio, by given width OR height (strictly only one of them).

    For version 3.0: cached images (with restricted sizes) ;)

    (I will do these for myself)

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.