How to resize and display database URL images using PHP?

Hi there,

I’m trying to display and re-size images using URL’s which are held in a Mysql database (under the column named “ImageUrl”),
the images are all from separate sources and so I need to re size them appropriately before displaying them in separate Divs, I’m still trying to get my head around a lot of PHP, so just to let you know that I’m no expert.

So far I have:
-Connected to the database
-Ran the query
-Then displayed the query result (So I can display the original image just fine by sourcing the link from the ImageUrl column, but its still massive)

So next, i suspect that I need to use the GB library to store the image after resizing it, before displaying it.
I have found find plenty of example’s online illustrating how to do this using an image which is already saved on the server, but none on how to do it using a URL sourced from a database.

And just in-case the question still seems hazy, my question is:
How do I use PHP to call upon and re-size a database URL Image?

Any replies will be greatly appreciated =D

Well you can do it that way… as long as you dont ever link to an image bigger than PHP’s memory limit.

PHP will have to load the entire image to be able to resize it.

[FPHP]file_get_contents[/FPHP] the url (if your webhost allows remote file_get_contents), [FPHP]imagecreatefromstring[/FPHP] the results, [FPHP]imagecreate[/FPHP] an image in the desired size, and then [FPHP]imagecopyresized[/FPHP] A into B. I would highly suggest then locally saving the result for quicker handling. (Note: Please make sure you have permission to copy said image from the remote site; we are not responsible for your copyright infringements!)

You can do it with Imagemagick

Page on its own called something like resize.php:


$image = $_GET["I"];
exec("convert $image -resize 500x500 JPG:-");

Where you want the image:


$I = result from the database query - should be a URL;
echo "<img src=\\"resize.php?I=$I\\">";

Thanks for both of your replies!
I attempted Lions approach but I don’t think Im experienced enough with those operators, well, not enough to put them all together anyway, although I did try :frowning:

Rubbles approach seemed easier, but I still cant seem to get it to work >_<
1.I created a new file in my folder called resize.php
2.I entered this code:
<?php
$image = $_GET[“I”];
exec(“convert $image -resize 500x500 JPG:-”);
?>
And then saved it.

  1. In my events.php file, in the specific Div i have entered this code:
    <?php
    $I = $attresult[‘ImageUrl’];
    echo “<img src=\“resize.php?I=$I\”>”;
    ?>

but I’m getting no image at all, or any error codes.

Just to help keep it clear, this is my code for calling the URL from the ImageUrl Column:
<?php
$query1 = “SELECT * FROM attraction WHERE AttractionID=1”;
$attquery = mysql_query($query1) or die('query error has occured ’ . mysql_error());
$attresult = mysql_fetch_assoc($attquery);
?>

I also use this code to pull the Name and Description Column’s info.

Thankyou!!!

Edited “Imageresizer” for “resize.php”

What do you get if you go to resize.php?l=<a valid thing>? I’m gonna go ahead and guess you’re being denied access to exec, as thats a very dangerous function to give people access to on a shared host.

Um…

When going to this address (The actual link is the target link from my database):
http://localhost/database/resize.php?l=http://4.bp.blogspot.com/_rOxJ-Rvzrmc/TQaQrO27omI/AAAAAAAAB5w/Icegb-PJxDk/s1600/Leeds%2BCorn%2BExchange.jpg

it says:
Notice: Undefined variable: attresult in C:\xampp\htdocs\database\resize.php on line 2

But I think my PHP is correct?

It is terrible as I can not remember how this works now as I only did it once :eek:

I think the $_GET is incorrect as you do not have a URL to “get” from. I will need to think about it for a while.

No problem! Thanks again for your help, I’l keep checking back! :smiley:

What is the content of resize.php ?

Copy, paste, and put [“PHP”] [“/PHP”] around it (take the quotes out). Cause what you showed us doesnt call for $attresult.

I have been trying to get this to work on my localhost and it would not - uploaded to my server and it does. There must be something in the setup. I see you are using a localhost as well; if it does not work I would try it on the server.

I also started with an image in the same folder as the code to get rid of any other problems; once this works add the database part etc. one bit at a time.

resize.php:


<?php

$Im = isset($_GET['I']) ? $_GET['I'] : 'default.png';

$cmd = "/usr/local/bin/convert $Im -thumbnail 200x200 ".
" -unsharp 0.2x0.6+1.0 -quality 100 JPG:-";

header("Content-type: image/jpeg");
passthru($cmd, $retval);

?>

main test page:


<?php
 $image = 'th__MG_6463.jpg';
 echo "<img src=\\"resize.php?I=$image\\">";
?>

I assume you have Imagemagick installed and you may need to change convert to something like /usr/local/bin/convert depending on how your server is setup.