Resizing Image Before Uploading

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Smudly
Forum Commoner
Posts: 71
Joined: Wed Jun 09, 2010 10:09 pm

Resizing Image Before Uploading

Post by Smudly »

Hi everyone,

I'm currently working on resizing an image that the user wants uploaded as their avatar. The resizing will occur before the uploading.

At the moment I'm just testing the resizing script without uploading it. It works great, however I need to make it so it will work with all image formats (at least the basics - jpg, png, gif, bmp). In the code below I tried changing the .jpeg and .jpg areas to .gif (abc.jpg Is Now abc.gif, image/jpeg Is Now image/gif, imagejpeg($thumb) is Now imagegif($thumb)), but it is giving me this error: "The image “http://www.mysite.com/testdelete.php” cannot be displayed, because it contains errors.".
The weird thing is, this error message is actually an image.

Am I going about this the right way? In the end, I will be needing to capture the resized picture in a variable to show to users on various pages. I noticed by using imagejpeg($thumb) it immediately shows it to the screen, but I would rather capture this into a variable to show in different areas of the page.

Any suggestions on where to go from here?

MySql Version 5.0.91
My Code:

Code: Select all

// File and new size
$filename = 'avatars/abc.gif';

// Content type

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



// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = 65;
$newheight = 65;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

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

// Output
imagegif($thumb);
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Resizing Image Before Uploading

Post by Jade »

What you need to do is determine what type of image it is (png, bmp, gif, jpg) and then do the same thing with the different image types: http://us3.php.net/manual/en/function.m ... t-type.php.

Code: Select all

$type = mime_content_type($filename);

header('Content-type: $type');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = 65;
$newheight = 65;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);


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

switch ($type)
{
            case "image/gif":
                       imagegif($thumb);
                       break;
            case "image/jpg":
            case "image/jpeg":
                       imagejpg($thumb);
                       break;
            case "image/png":
                       imagepng($thumb);
                       break;
            case "image/bmp":
                       imagebmp($thumb);
                       break;
}
?>
Post Reply