php image resize on the fly help

Started on the old forum in August 2015 · view the original in the archive · old links redirect here

2 replies · 89 views · started 11 years ago · 1 min read

2 participants
Android404 Junior · 15 posts #1

Hi there.

I need some help here with something...

Has someone perhaps got some coding for a php thumbnail script? On the fly, meaning that you can resize images Example: image.php?imgdir=$whatever

And it will resize the image to, lets say 640 x 480 or whatever, but still keep aspect ratio. I did play around with some coding, but cant seem to work around keeping the aspect ratio.

Thank you in advance. Quintin

GumSlone Administrator Principal · 1,690 posts #2

found some old simple script that i have used long ago

<?php
if( is_array($_REQUEST) )
    {
        while( list($k, $v) = each($_REQUEST) )
        {
            if( is_array($_REQUEST[$k]) )
            {
                while( list($k2, $v2) = each($_REQUEST[$k]) )
                {
                    $$k[$k2] = addslashes($v2);
                }
                @reset($_REQUEST[$k]);
            }
            else
            {
                $$k = addslashes($v);
            }
        }
        @reset($_REQUEST);
    }

$size = getimagesize($filename);
if(($size['mime'] == 'image/gif'||$size['mime'] == 'image/jpeg'||$size['mime']=='image/png')&& !empty($filename))
{
if($height>500)$height= 500;
if($width>500)$width= 500;
if(!$height)$height= 128;
if(!$width)$width= 128;

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

$width_orig=$size[0];
$height_orig=$size[1];

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

// Resample
$image_p = imagecreatetruecolor($width, $height);
if($size['mime']=="image/gif")$image = imagecreatefromgif($filename);
if($size['mime']=="image/jpeg")$image = imagecreatefromjpeg($filename);
if($size['mime']=="image/png")$image = imagecreatefrompng($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 80);
ImageDestroy ($image_p);
}
else
{

}
?>

usage example : image.php?filename=image/dir/pic.jpg&width=300&height=300

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Android404 Junior · 15 posts #3

thanks for your reply...

I actually sat and worked around the problem i had. using a class upload script which does most of the work for me.

Then I'm using another thumb.php script that i've done just to make pics a bit smaller for the wap version of the website i coded.

Thanks a lot though.

Quintin

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close