echo "<img src='resize-image.php?image=".$filename."'>";

resize-image.php:

<?php

//destroy image first as the img tag that calls it is in a loop
ImageDestroy($src);
ImageDestroy($dst);

if (!$max_width)
$max_width = 150;
if (!$max_height)
$max_height = 100;

$size = GetImageSize($image)or die("Could not get image size");
$width = $size[0];
$height = $size[1];

$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;

if (($width <= $max_width) && ($height <= $max_height))
{
$tn_width = $width;
$tn_height = $height;
}
else if (($x_ratio $height) < $max_height)
{
$tn_width = $max_width;
$tn_height = ceil($x_ratio
height);
echo "<br>tn_height: ".$tn_height;
}
else
{
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}

$src = ImageCreateFromJpeg($image)or die("Could not create from Jpeg");
$dst = ImageCreateTrueColor($tn_width, $tn_height)or die("Could not create new image");
ImageCopyResized($dst, $src, 0,0,0,0, $tn_width, $tn_height, $width, $height)or die("Could not copy resized");

header('Content-type: image/jpg');
imagejpeg($dst, null, 75);
?>

However no image is returned. In firefox nothing and in IE just the red cross box. That box shows the image src as resize-image.php?

I've tried moving teh header around but it doesn't work at the top of resize-image.php or at the top of the page that calls it.

Any ideas?

    You are assuming register_globals in On.

    Set $image ... don't just pull it out of thin air. Also, why are you destroying $src and $dst when they don't exist?

      Register Globels is on.

      I'm destroying $src and $dst because the <img> is in a loop and I wanted to make sure they were empty before creating them later in the script.

      $filename contains the path of an image stored on the server. Before trying to resize the image I got it to display the original image and that worked so I know the address is correct.

      I've tried
      $src = ImageCreateFromJpeg($image)
      and I've tried
      $src = ImageCreateFromJpeg($_GET["image"])

      Viewing the source of the page and copying resize-image.php... from the code and pasting it into the address bar shows up all the gibberish of the file with no error messages so I know it's creating the resized image.

      I don't know why the <img> isn't displaying it though.

        This is included within an HTML page that contains other code including start_session() at the top and the template HTML for the page layout. This is inside a <td>.
        It displays photos ordered by catagory with a header for each catagory. The images have been uploaded by a user and so will be large. I'm trying to display them as thumbnails that you can then click to display the large image. I'll add that code once I get the thumbnails to work.

        <?php
        // include the database connection
        include ("Includes/connection.php");

        $query = "SELECT photo, catagory FROM photos GROUP BY catagory";
        $result = mysql_query($query,$db)or die("Could not complete query ".mysql_error());

        while ($row = mysql_fetch_array($result))
        {
        $var = $row['catagory'];
        echo "<h3>".$var."</h3>";
        $query2 = "SELECT photo, username FROM photos, users WHERE catagory = '".$var."' AND photos.userID = users.userID";
        $result2 = mysql_query($query2, $db)or die("<br>Could not query database2<br>".mysql_error());

        echo "<table><tr>";
        while ($row2 = mysql_fetch_array($result2))
        {
        	$image = "/var/www/vhosts/stoneycove.com/httpdocs/New Site/Images/Users Gallery/".$row2['photo'];
        	echo "<td valign='bottom' align='center'><img src='resize-image.php?image=".$image."'><br>".$row2['username']."</td>";
        }
        echo "</tr></table>";

        }
        ?>

        This then points to resize-image.php which has the code I showed earlier:
        <?php

        //destroy image first as the img tag that calls it is in a loop
        if ($src) ImageDestroy($src);
        if ($dst) ImageDestroy($dst);

        if (!$max_width)
        $max_width = 150;
        if (!$max_height)
        $max_height = 100;

        $size = GetImageSize($image)or die("Could not get image size");
        $width = $size[0];
        $height = $size[1];

        $x_ratio = $max_width / $width;
        $y_ratio = $max_height / $height;

        if (($width <= $max_width) && ($height <= $max_height))
        {
        $tn_width = $width;
        $tn_height = $height;
        }
        else if (($x_ratio $height) < $max_height)
        {
        $tn_width = $max_width;
        $tn_height = ceil($x_ratio
        height);
        echo "<br>tn_height: ".$tn_height;
        }
        else
        {
        $tn_width = ceil($y_ratio * $width);
        $tn_height = $max_height;
        }

        $src = ImageCreateFromJpeg($image)or die("Could not create from Jpeg");
        $dst = ImageCreateTrueColor($tn_width, $tn_height)or die("Could not create new image");
        ImageCopyResized($dst, $src, 0,0,0,0, $tn_width, $tn_height, $width, $height)or die("Could not copy resized");

        header('Content-type: image/jpg');
        imagejpeg($dst, null, 75);
        ?>

          did you check the url of the image in the browser?

          for instance paste the following

          "resize-image.php?image=/var/www/vhosts/stoneycove.com/httpdocs/New Site/Images/Users Gallery/myImage.jpg"

          into the url and see what happens. You should see a resized version of your image in the browser. If not then something is wrong with your script.

            OK, if I change the following bit of code

            if (!$max_width)
            $max_width = 150;
            if (!$max_height)
            $max_height = 100;

            to

            if (!$max_width)
            $max_width = 200;
            if (!$max_height)
            $max_height = 150;

            the direct url shows the reduced image.

            If I set them both to 150 it says "Could not create new image".

            The original test image is 210 x 148px

            So I guess there's something wrong with my resize-image.php script. I copied it from SAMS PHP and MYSQL Web Development so I'm just trying to get my head around their if statements.

              aaah I just got done writing a really thorough explanation and the damn thing logged me out and I lost it all!!!!!!

              any way I basically said this. . .

              Your problem seems to originate in the imagecreatetruecolor function.

              http://us.php.net/imagecreatetruecolor

              I don't recommend resizing images down to a square as they will get distorted (unless the image is as a square to begin with). I recommend that you crop them first. I have a tutorial that shows you how to do this.

              http://www.partdigital.com/tutorials/gd-thumbnail

              if you're doing this to learn php you might want to start with something more simple. gdLibrary can be pretty hard.

                I'm doing this for work and am familiar with PHP but it is the first time I've tried to do any kind of image manipulation with PHP.

                I thought that using the X_ratio and y_ratio variables meant that one side would be resized to a set size ($max_width or $max_height) and the other size would then be resized using the ratio variable ($X_ratio or $y_ratio multiplied by the $height or $width). I'm then creating an image based on these calculated results.

                If I use ImageCreate without the TrueColor then the image doesn't display properly.

                Scrub that. It appears to be working now.
                Thanks.

                And thanks for those turorials, they're great, really explained clearly. :-)

                  Yeah imagecreate is deprecated, you're better off using imagecreatetruecolor.

                  glad you got it to work, it can be a pain in the butt 🙂

                    Sure can! But it feels great when it finally works just the way you want it to. :-D

                    Thanks again.

                      Write a Reply...