[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 434
  • Last Modified:

Is there a way to resize jpg images on the fly from databased image locations

Using the fabulous FREE PHP upload and image gallery script (35mm Slide Gallery  - http://www.andymack.com/freescripts/) I have created an image gallery which I use to browse product images for simple 'directory/filename' entry into a field in the products table of a MySQL database.

1) With the 'directory/filename' values stored in my database I want to call the images, as required, into a dynamic PHP page from their referenced locations.

Sometimes I require them as thumbnails and sometimes as larger images depending on the page they are called to.

Without storing various 'sized' copies of the original images on the server, is there a way for PHP to code on the fly using some fancy 'functions' that will resize (in ratio) the images as they are referenced from the database.

Also...

As great as the 35mm Slide Gallery Script is it offers no way of deleting individual files created in the galleries. Is there a simple way I could add under each thumbnail a link that would delete the image from the directory completely. (All images are jpg format -  but there is also an associated .txt file by the same filename created when the file is uploaded.)

Look forward to your ideas!

Tim
0
Mediatricks
Asked:
Mediatricks
  • 10
  • 6
1 Solution
 
ramu_src2kCommented:
<?php
function ImageResize ($imgfile, $percent)
{
header('Content-type: image/jpeg');

list($width, $height) = getimagesize($imgfile);
$newwidth = $width * $percent;
$newheight = $height * $percent;

$thumb = ImageCreateTrueColor($newwidth,$newheight);
$source = imagecreatefromjpeg($imgfile);

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg($thumb);
}
?>

You can call this function and display the image $imgfile wherever you want with different $percent values. Here the image is not modified, it is kept as original.
Cheers...
0
 
MediatricksAuthor Commented:
Will give it a shot tonight and let you know!
0
 
MediatricksAuthor Commented:
Have a couple of problems..

If I create a php page called testimage.php using your function followed by:

$bd1 = "admin/images/England/2.jpg";
$bd2=ImageResize($bd1,0.5);

echo "<img src=\"$bd3\" alt='Hoorah'>";

I do get the resized image - good work! I  can't seem to run the function itself 'within' the image tag as I would like though. eg <img src=ImageResize($image)>

Is this just my ineptness or a PHP restriction?

There is another problem though...

If I add any other PHP code under that echo image line nothing gets displayed.

I added: echo "<img src=\"$bd1\" alt='Original Size'>"; under the last line in my page above and all that displays is the first resized thumbnail.

Doesn't bode well for including these images within a formatted page. Any ideas?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ramu_src2kCommented:
imagejpeg($thumb) outputs the image to browser or to a file. Instead of using imagejpeg(), use echo for the newwidth and newheight values, modifying the function as:

<?php
function ImageResize ($imgfile, $percent)
{
header('Content-type: image/jpeg');

list($width, $height) = getimagesize($imgfile);
$newwidth = $width * $percent;
$newheight = $height * $percent;
echo "width=\"$newwidth\" height=\"$newheight\"";
}
?>
Now, you can use this function whereever you want as:
<img src="admin\images\England\2.jpg" <?php ImageResize('admin\images\England\2.jpg', 0.5); ?> alt="">
<img src="admin\images\England\2.jpg" <?php ImageResize('admin\images\England\2.jpg', 0.75); ?> alt="">
<img src="admin\images\England\2.jpg" <?php ImageResize('admin\images\England\2.jpg', 1); ?> alt="">
<img src="admin\images\England\2.jpg" <?php ImageResize('admin\images\England\2.jpg', 2); ?> alt="">

Both of your problems are solved now, because I tested this function with different images. I hope , it helps you..
Cheers..
0
 
MediatricksAuthor Commented:
Doesn;t work. Get the error message

Warning: Cannot modify header information - headers already sent ... on line 4

and

Warning: getimagesize(/images/England/2.jpg): failed to open stream: No such file or directory ... on line 6

Plus all this function would do is rezise the image based on attributes isn't it. I need to resize the jpg so that page loading time is not slowed by large source images needed as small thumbnails.

Can anyone provide me with a saaple script that would create a table 2 col by 2 rows where in each cell a different image (Let's call them $image1, $image2, $image3 and $image4 - which all have the exact same origianl dimensions) are displayed at 100%, 75%, 50% and 25% of their origianl size.

0
 
ramu_src2kCommented:
If you see errors like this:

Warning: Cannot add header information - headers already sent by (output started at /webs/moodle/config.php:87) in /webs/moodle/lib/moodlelib.php on line 1322

Warning: Cannot add header information - headers already sent by (output started at /webs/moodle/config.php:87) in /webs/moodle/lib/moodlelib.php on line 1323

Warning: Cannot add header information - headers already sent by (output started at /webs/moodle/config.php:87) in /webs/moodle/login/index.php on line 54

..then you have blank lines or spaces after the final ?> in your config.php file. Sometimes text editors add these - for example Notepad on Windows - so you may have to try a different text editor to remove these spaces or blank lines completely.

If your server is configured not to show error messages, then the only symptoms might be that all images appear corrupted (broken).
--------------
Try this .. keep the function ImageResize() as it is, but comment the header() as below:

<?php
function ImageResize ($imgfile, $percent)
{
//header('Content-type: image/jpeg');

list($width, $height) = getimagesize($imgfile);
$newwidth = $width * $percent;
$newheight = $height * $percent;
echo "width=\"$newwidth\" height=\"$newheight\"";
}
?>

Now, write a function to display an image in 4 dimensions.. in a table as u said,

<?php
function display($imgfile)
{
echo ("<table align=\"center\" border=\"1\">
            <tr>
                  <td align=\"center\">
                  <img src=\"$imgfile\" "); ImageResize($imgfile, 1);echo (" alt=\"100%\">
                  </td>
                  <td align=\"center\">
                  <img src=\"$imgfile\" "); ImageResize($imgfile, 0.75);echo (" alt=\"75%\">
                  </td>
            </tr>
            <tr>
                  <td align=\"center\">
                  <img src=\"$imgfile\" "); ImageResize($imgfile, 0.5);echo (" alt=\"50%\">
                  </td>
                  <td align=\"center\">
                  <img src=\"$imgfile\" "); ImageResize($imgfile, 0.25);echo (" alt=\"25%\">
                  </td>
            </tr>
         </table>");
}
?>

Call the function wherever you want, for example..

<?php
display("rose.jpg");
echo ("<br><br><br>");
for($i=1;$i<=10;$i++)
{
echo("<img src=\"rose.jpg\" ");
ImageResize('rose.jpg', $i/10);
echo (" alt=\"\">");
}

?>

Cheers...
0
 
MediatricksAuthor Commented:
OK - that works but the filesize of the images is not affected.

I want to use the PHP image create functionality so that if a staff memebr has uploaded a large product image file to the images folder, when it is called to the web page for display it will be recreated as smaller filesize as well as dimensions.A sort of dynamic thumbnailing

Does this make any sense?
0
 
MediatricksAuthor Commented:
This is where I am up to:

Using your first suggestion I created a page called resizeimage.php

<?php
function ImageResize ($imgfile, $percent)
{
//header('Content-type: image/jpeg');

list($width, $height) = getimagesize($imgfile);
$newwidth = $width * $percent;
$newheight = $height * $percent;

$thumb = ImageCreateTrueColor($newwidth,$newheight);
$source = imagecreatefromjpeg($imgfile);

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg($thumb);
}
$img=$row_DetailRS1['image2'];
$path="images/";
$imgfile="$path$img";
ImageResize($imgfile, 0.25);
?>

where I need the image in my documents I have tried calling them using

<?php
include ("resizeimage.php");?>

The resizeimage.php page works fine but when 'included' into the existing pages I am getting the image file in ASCII format I think (just a bunch of gobbledygook!)

Is there a quick fix to turning the included page into an image file recognizable by the web browser.
0
 
MediatricksAuthor Commented:
Am increasing the points to crack this as have searched so many solutions on the Exchange for a simple answer to this - and please dont say ImageMagik!!


0
 
ramu_src2kCommented:
I wrote the function ImageResize() wrongly by placing a header() inside it. Any way without the statement it is working na.. well write the function in a file called "includes.inc". So, instead of naming the file "resizeimage.php", name it as "includes.inc" or "resizeimage.inc".

Now, in the file where you want to call the function, on top include the file as..
<?php
include "includes.inc";
?>
This works well...
otherwise, if u name the file with extension .php, you should require the file as:
<?php
require("includes.php");
?>
Try whatever you want..
Cheers..
0
 
MediatricksAuthor Commented:
No change to the output. Still get the image displayed as follows

ÿØÿàJFIFÿþ>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ÿÛC    $.' ",#( etc.

I can smell success  - if I am this close it must be possible to turn the gd-jpeg data into an image.

PLease...................
0
 
MediatricksAuthor Commented:
This looks like a header issue but if I uncomment the header line I get a Header Warning as the headers were set early in the page in which the image is being displyed
0
 
ramu_src2kCommented:
Here, I tested online.
http://sirisana.si.ohost.de/test/
At the above link, you can see various files. Check it once..
...
0
 
MediatricksAuthor Commented:
Ramu,

Thanks for the links. I know it works with the function in your include file and on the test2.php page in your directory but that is only changing the dimensions of the image display. I can do that in the html page simply by setting the height or width attributes in the src tag.

The issue is this.... I have product managers uploading images to the gallery which are called from their link information in the database. Some managers have no IT skills as to resizing/sampling images and if they uplaod a 800 x 600, 500Kb file and I only need it viewed as a thumbail on a page I dont want web visitors to have to download the large original image file. I want to use the imagecreate function to re-sample and resize the image before it is displayed to the web page.

Your first function does the resampling and displays the image correctly as a result as ling as there is no other content on the page. That is why I tried calling that complete file into another using include (or require) but somwehere along the line it loses it's format.

Do you see what I am getting at here. It's basically a thumbnailing option required. I want to be able to scale the original pictures based on the original width or height and do not want to store them on the server - just create them as required.
0
 
MediatricksAuthor Commented:
Ok here goes - my own solution that seems to work

Put this function in the page...

<?php
function thumbnail ($img, $max) \\ $max is the maximum vertical or horizontal size of the new image
{
$a_img = $img;
$imgdir="http://www.xxxxxx.com/images";
$size = getimagesize ("$imgdir/$a_img");

if($size[1] < $size[0])
{
$width = $max; \\ For landscape images this is the horizontal size you want the new image to be.
$percent = $width/$size[0];
$height = $size[1]*$percent;
}
else
{  
$height = $max; \\For portrait images this is the vertical size you want the new image to be.
$percent = $height/$size[1];
$width = $size[0]*$percent;
}

$thumbnail = "images/thumbs.php?image=$imgdir/$a_img&newheight=$height&newwidth=$width&width=$size[0]&height=$size[1]";

print "<img src=\"$thumbnail\" height=$height width=$width border=0 alt='$a_img'>";
}


And call each image location from the database as follows...

echo thumbnail($row_DetailRS1['image_reference'],200); \\ 200 to create images with a maximum side of 200 pixels

This links to the thumbnailing script thumbs.php as follows


<?php
#thumbs.php
$image = $_GET['image'] ;
$newwidth = $_GET['newwidth'];
$newheight = $_GET['newheight'];
$height = $_GET['height'];
$width = $_GET['width'];


$src = imagecreatefromjpeg("$image");
$im = imagecreatetruecolor($newwidth,$newheight);  
imagecopyresampled($im,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagejpeg($im, '',85);
imagedestroy($im);

?>


All works great.

Function checks the original image for portrait or landscape orientation and scales it in proportion to the original size to meet the size specified in the function.

The thumbs.php does the image manipulation and the image displays in place on the page from the function call.

By calling each image reference through the thumbnail function you can recreate and display any images you want, to any size, on the fly.

0
 
ramu_src2kCommented:
Very Nice... Good Work!
Cheers..
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 10
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now