...

View Full Version : Picture resize and resave from forum



dawg
11-18-2011, 08:26 AM
I have a method that grabs a image from a forum, saves it, puts it in the database, and it works fine. I just want to resize it without opening the file or anything.

Which of these Would I resize?
I thought it was the tmp_name, but that just makes a huge error.

$fileName = $_FILES['Photo']['name'];
$tmpName = $_FILES['Photo']['tmp_name'];
$fileSize = $_FILES['Photo']['size'];
$fileType = $_FILES['Photo']['type'];

Any ideas would be great.

Thanks

timgolding
11-18-2011, 11:09 AM
You can't resize the image this way. If you want the actual image to be resized you will need to use something like ImageMagick (http://www.imagemagick.org/) or the GD library (http://www.boutell.com/gd/). clicking the links will give you documentation on how to install these. If you are with a hosting company then you need to check whether they have any of these extensions installed for your use. If not you might be stuck with the images the size they are and just use html to display the images at different sizes with the width attribute. If you have got GD let us know!

Truffle
11-18-2011, 04:09 PM
Here's a function I made to resize images and keep the aspect ratio

It uses constants that I defined in an include
IMG_RESIZE_HEIGHT and IMG_RESIZE_WIDTH just replace those values.


But this stores the images on your filesystem rather than the database but I think you could still make it work.
Oh yeah, and it requires GD like timgolding mentioned.

$source = path/to/original/image
$destination = /path/to/be/saved




function resize_image( $source, $destination, $thumbnail = false )
{
list( $width, $height, $type ) = getimagesize( $source );

switch( $type ) {
case IMAGETYPE_GIF:
$image = imagecreatefromgif( $source );
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg( $source );
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng( $source );
break;
}

if( $image === false )
return false;

$aspect_ratio = $width / $height;
if( $thumbnail === true ) {
if( $width > IMG_PREVIEW_WIDTH )
$new_width = IMG_PREVIEW_WIDTH;
else
$new_width = $width;

if( $height > IMG_PREVIEW_HEIGHT )
$new_height = IMG_PREVIEW_HEIGHT;
else
$new_height = $height;
}
else {
if( $width > IMG_RESIZE_WIDTH )
$new_width = IMG_RESIZE_WIDTH;
else
$new_width = $width;

if( $height > IMG_RESIZE_HEIGHT )
$new_height = IMG_RESIZE_HEIGHT;
else
$new_height = $height;
}

if( $width > $height )
$new_height = (int) $new_width / $aspect_ratio;

if( $height > $width )
$new_width = (int) $new_height * $aspect_ratio;

$new_image = @imagecreatetruecolor( $new_width, $new_height );
imagecopyresampled( $new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

switch( $type ) {
case IMAGETYPE_GIF:
imagegif( $new_image, $destination );
break;
case IMAGETYPE_JPEG:
imagejpeg( $new_image, $destination, 100 );
break;
case IMAGETYPE_PNG:
imagepng( $new_image, $destination, 4 );
break;
}
}

mvmacd
11-18-2011, 04:26 PM
Check out this link:

http://www.les-stooges.org/pascal/webdesign/vignettes/index.php?la=en

I use this to resize images on-the-fly. Download the script, place it in your main directory http directory. I forget the original name but I named mine img.php. Then in your website add



<img src='/img.php?f=path/to/image.jpg&amp;w=100' />


Or you can specify &h=100 for 100px height. It also has shadows and frame options!

One note, if you enable caching and try to change the shadow/frame/width/height, you might need to clear the temp directory for it to reflect changes.

dawg
11-18-2011, 06:54 PM
Awesome man, I spent 4 hours trying to get something that works, yours is beautiful.

Thanks a million,
Dawg



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum