Rob Wiltbank

Educator. Technologist. Web Developer.

Google +PinterestLinkedinTwitterRSS
  • Home
  • Publications & Research
  • Resume
  • About Rob
  • Contact Rob

Resizing Images on the Fly with PHP

Posted in: PHP, Web Development November 2nd, 2014

Tweet about this on Twitter
Twitter
Pin on Pinterest
Pinterest
Share on Facebook
Facebook
Share on Reddit
Reddit
Email this to someone
email

Resizing Images“Why the hell am I writing articles in a blog?”  That’s literally what I asked myself the other day.  “Self,” I replied, “I want to help the world understand PHP and concepts of education better with the hopes making everyones lives better.”

“Liar,” I shot back… to myself, “You’re just getting old and want some place to put things you’ve figured out so you don’t forget them.”

Sigh.

So, in this episode of “Crap I Don’t Want To Figure Out Again”, I found myself in need of taking an image and resizing it using PHP.  This isn’t a very common task for PHP, as developers usually have static images and service them in a typical fashion, so this took a bit of research.

I did, however, have a level of difficulty…  These were ID badge photos coming out of a BLOB object in an Oracle database, all of which were different sizes and I needed a 100×100 image.  The nice thing is that, since these were ID badge photographs, I could generally depend on the face being fairly centered in the image.  The whole, “Getting a binary image out of a database to use” will be a different article, but this will handle taking an image and resizing it.

//Here's some sample image data.
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';

//Create the image from the string.
$img = imagecreatefromstring(base64_decode($data));

//Set the desired height and width.
$thumb_width = 100;
$thumb_height = 100;

//Measure what we have.
$width = imagesx($img);
$height = imagesy($img);

//Do some calculations.
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;

//Figure out the dimensions we need.
if ($original_aspect >= $thumb_aspect)
{
    // If image is wider than thumbnail (in aspect ratio sense)
    $new_height = $thumb_height;
    $new_width = $width / ($height / $thumb_height);
}
else
{
    // If the thumbnail is wider than the image
    $new_width = $thumb_width;
    $new_height = $height / ($width / $thumb_width);
}

//Create the image template.
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);

//Resize down and crop from the center.
imagecopyresampled($thumb,
$img,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);

//Save the finished product at 100% quality to a file.
imagejpeg($thumb, "photos/imagefile.jpg", 100);

//Free up the resources.
imagedestroy($img);
imagedestroy($thumb);

That’s the short and skinny of it. It’s more of a syntax and aspect ratio puzzle rather than anything hugely difficult.

Tweet about this on Twitter
Twitter
Pin on Pinterest
Pinterest
Share on Facebook
Facebook
Share on Reddit
Reddit
Email this to someone
email

Tags: image, manipulation, php

Leave a Comment Cancel

You must be logged in to post a comment.

Blog Post Categories

  • General (1)
  • Instructional Technology (1)
  • JQuery (1)
  • Oracle (1)
  • Pedagogy (2)
  • PHP (3)
  • Strategies (1)
  • Teaching (1)
  • Web Development (3)

Keyword Cloud

ajax cli command line facebook google+ image jquery lesson planning manipulation oracle pedagogy php social media teaching technology tpack twitter Web Development

Follow Pinterest's board Pin pets on Pinterest.
artificial grass for putting greens