Jump to content

Mysql Image Resize


Guest

Recommended Posts

And to suggest which functions to use with the data you pulled from your database:
[url=http://se2.php.net/function.imagecreatefromstring]http://se2.php.net/function.imagecreatefromstring[/url]
The function will autodetect what type (jpg/gif/png/etc.) of image it is in your variable and create it. Then you can just manipulate it with the other GD functions.

With the functions
[url=http://se2.php.net/manual/en/function.imagesx.php]http://se2.php.net/manual/en/function.imagesx.php[/url]
[url=http://se2.php.net/manual/en/function.imagesy.php]http://se2.php.net/manual/en/function.imagesy.php[/url]
you can retrieve the image width and height.

Good luck! :)
Link to comment
https://forums.phpfreaks.com/topic/33538-mysql-image-resize/#findComment-157194
Share on other sites

I cant quite get it working.

[code]
// Load image
$img = $row[$imgnum];

if($mwidth != "" && $mheight != "")  {

    // Get image size and scale ratio
    $width = imagesx($img);
    $height = imagesy($img);
    $scale = min($mwidth/$width, $mheight/$height);

    // If the image is larger than the max shrink it
    if ($scale < 1) {
        $new_width = floor($scale*$width);
        $new_height = floor($scale*$height);

        // Create a new temporary image
        $tmp_img = imagecreatetruecolor($new_width, $new_height);

        // Copy and resize old image into new image
        imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
                        $new_width, $new_height, $width, $height);
        imagedestroy($img);
        $img = $tmp_img;
    }

        header("Content-type: $imagetype");
echo $img;

} else {

        // set the header for the image
        header("Content-type: $imagetype");
        echo $img;
}
[/code]

but the results are

The image “http://www.bestorlandorealty.com/inc/view.php?ID=5&type=house&image=1&wid=100&hei=100” cannot be displayed, because it contains errors.

The image is fine if i just display it I dont know whats wrong?
Link to comment
https://forums.phpfreaks.com/topic/33538-mysql-image-resize/#findComment-157721
Share on other sites

Well, I think I know what is wrong.
I assume you've stored the image file (jpg, gif, png or so) in the database.

Then, you must load the image into the GD. The image resource (which should be $img) and the image file ($row[$imgnum]) is in VERY different format. Try like this instead.

[code]

if($mwidth != "" && $mheight != "")  {

    // Load image to GD from the data
    $img = imagecreatefromstring($row[$imgnum]);

    // Get image size and scale ratio
    $width = imagesx($img);
    $height = imagesy($img);
    $scale = min($mwidth/$width, $mheight/$height);

    // If the image is larger than the max shrink it
    if ($scale < 1) {
        $new_width = floor($scale*$width);
        $new_height = floor($scale*$height);

        // Create a new temporary image
        $tmp_img = imagecreatetruecolor($new_width, $new_height);

        // Copy and resize old image into new image
        imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
                        $new_width, $new_height, $width, $height);
        imagedestroy($img);
        $img = $tmp_img;
    }

        // If you really wish for the image to be of the same format as $imagetype,
        // then you can make a switch-case here. Now I set it so send the thumb as jpeg
        header("Content-type: image/jpeg");
imagejpeg($img);

} else {

        // set the header for the image
        header("Content-type: $imagetype");
        echo $row[$imgnum];
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/33538-mysql-image-resize/#findComment-158243
Share on other sites

(Ah, just as an addition)
Ofcourse, as I wrote it now, it will load the image into the GD and then send it as a JPEG no matter if it was scaled down or not, as long as a width and height was given. My suggestion was just to give you an idea how GD works.

Hope it helps you along the way!
Link to comment
https://forums.phpfreaks.com/topic/33538-mysql-image-resize/#findComment-158247
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.