problems resizing images with PHP and GD

images php coding issues or problems here.

Moderators: macek, egami, gesf

maolu
New php-forum User
New php-forum User
Posts: 3
Joined: Mon Jul 18, 2005 11:56 pm

problems resizing images with PHP and GD

Postby maolu » Mon Jul 18, 2005 11:59 pm

Hi everybody!
I have a prob with php+GD...
I want to show, into my php page, thumbnails of the images i have onto my server.
I'm using this function, as found in php.net:


Code: Select all

function resizeImage($filename)
    {
        $width = 50;
        $height = 50;

        header('Content-type: image/jpeg');

        list($width_orig, $height_orig) = getimagesize($filename);

        if ($width && ($width_orig < $height_orig))
        {
            $width = ($height / $height_orig) * $width_orig;
        }
        else
        {
            $height = ($width / $width_orig) * $height_orig;
        }

        $image_p = imagecreatetruecolor($width, $height);
        $image = imagecreatefromjpeg($filename);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
       
        imagejpeg($image_p, null, 100);
    }


Everything goes well, and if i try do do a resizeImage("img.jpg") i can correctly see the thumb.
My problem comes when i have to show a list of images whose URLs i get from a MySQL query.

If i do this:

Code: Select all

$subQuery = "SELECT * FROM items WHERE tipo=1";
    $subResult = mysql_query($subQuery, $db);

    while ($subRow = mysql_fetch_array($subResult))
    {
        resizeImage ("../".$subRow[url]);
        echo $subRow[url];
    }


my thumbnail is shown in ASCII!!!!
If i don't insert the "echo line", my thumb is show correctly!

Instead of the images i get something like this:

ÿØÿàJFIFÿþ|Õµ_Í�Œü];xWÃ:¦ØØèS]
M6¥âA¿»–MMŠiìãÛ ”¶KÛ,Ê}ƒãŸí«û< þÍ7‰¥|bñkxoU›C›Ä°i‘xoǺ_̺ÐɨyÚW‚ï,£€O
Û§�y’L_ +Kòæ/þ


Anyone can help me?
Where's the mistake?

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Sun Jul 24, 2005 1:43 am

you might need to send a header with image/jpeg content type information
Or make a page like images.php and when calling images.php?id=3 it will fetch the info from the DB and send it to the page :) it will help :)

maolu
New php-forum User
New php-forum User
Posts: 3
Joined: Mon Jul 18, 2005 11:56 pm

Postby maolu » Sun Jul 24, 2005 8:28 am

i solved making a different page for simple resizing named resize.php and calling it from within the other page this way:

Code: Select all

echo ("<IMG SRC=\"resize.php?url=".$row[url]."\">);

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Sun Jul 24, 2005 9:22 am

almost as i told you... with different words.. hehe.. :)

maolu
New php-forum User
New php-forum User
Posts: 3
Joined: Mon Jul 18, 2005 11:56 pm

Postby maolu » Sun Jul 24, 2005 11:09 pm

LOL!
Sorry didn't understand... :D

My problem was that i'was ALREADY using the header-method, but inserting an "echo" line before or after the thumbnailing function was causing all that mess (just like the echo command caused the header line to "vanish"); that's why choose the separatedPage-method :)


Return to “PHP coding => Images”

Who is online

Users browsing this forum: No registered users and 1 guest