Jump to content

Recommended Posts

I'm calling a web resource to retrieve a base64 encoded image, and then re-sizing it into a small thumbnail to insert into the site header.

 

Most examples of PHP image re-sizing/display I've seen online assume you want to display the image on a page by itself using the header function. The page I'm working on has tons of content, so I don't want to get the headers already sent error. I want to set up the thumbnail within an IMG tag and don't want to save the image thumbnail to a file.

 

I stumbled upon a comment someone made on php.net that is doing the trick for me in a test environment. It suggests using a separate file (thumbnail.php) to display the image via imagepng(), then in the original page (testpage.php) use an IMG tag to call thumbail.php.

 

However, I wanted to see if this method was the most efficient way of accomplishing this. Since I didn't see a lot written about this specific need I have, I wanted to double-check here to see if anyone had a better idea.

 

Here is the code that works to 1) re-size a large image into a small thumbnail, and 2) displaying the image on a test page with other content.

 

thumbnail.php

// Decode base64 encoded image into Image
$imgDecoded = base64_decode($userImage64);

// Requires string image as parm, returns image resource
$im = imagecreatefromstring($imgDecoded);

// Get width and height of original image resource
$origWidth = imagesx($im);
$origHeight = imagesy($im);

// Create new destination image resource for new 24 x 24 image
$imNew = imagecreatetruecolor(24, 24);

// Re-sample image to smaller size and display
imagecopyresampled($imNew, $im, 0, 0, 0, 0, 24, 24, $origWidth, $origHeight);
imagepng($imNew);
imagedestroy($im);
imagedestroy($imNew);

 

Then in the content page:

 

<p>Here is some content</p>
echo "<img src=\"thumbnail.php\">";
<p>Here is some more content</p>

 

Since I'm the only PHP programmer here, there's no-one to bounce this off of, so if anyone has thoughts on a better or more efficient way to do this, please feel free. Thanks...

Sorry, I just posted a slice of it. Yes, I do have a "header" statement at the top of thumbnail.php. There are some curl commands in the file I left out for readability. Here's the code minus the curl commands:

 

thumbnail.php


<?php
header("Content-type: image/png");

// curl stuff
.
.
.
// Decode base64 encoded image into Image
$imgDecoded = base64_decode($userImage64);

// Requires string image as parm, returns image resource
$im = imagecreatefromstring($imgDecoded);

// Get width and height of original image resource
$origWidth = imagesx($im);
$origHeight = imagesy($im);

// Create new destination image resource for new 24 x 24 image
$imNew = imagecreatetruecolor(24, 24);

// Re-sample image to smaller size and display
imagecopyresampled($imNew, $im, 0, 0, 0, 0, 24, 24, $origWidth, $origHeight);
imagepng($imNew);
imagedestroy($im);
imagedestroy($imNew);

Gotcha and I did actually read your tl;dr as far as it being the most efficient, potentially. If you are going to use the thumbnail a ton, it might be worth it to run the script once and save it as a static file on the server. That should save processing time. But depending on the server and the number of hits it may or may not be necessary.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.