homepage Welcome to WebmasterWorld Guest from 54.234.2.88
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Upload image and create thumbnail
Uploading images to mysql database. Also creating thumbnails for the images
matthewamzn




msg:3749242
 11:22 pm on Sep 21, 2008 (gmt 0)

I'm trying to upload an image and it's thumbnail to my database. I'm stuck on resizing the image. This successfully uploads an image, but the resize fails.

$photo = addslashes(fread(fopen($_FILES['file']['tmp_name'], "r"),
$_FILES['file']['size']));

$thumb = imagecreatefromstring($photo);
$width = imagesx($thumb);
$height = imagesy($thumb);
$thumb_height = 100;
$thumb_width = $width / $height * $thumb_height;
$thumbnail = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumbnail,$thumb,0,0,0,0,$width,$height,ImageSX($thumb),ImageSY($thumb));

$vehicle_id = $_POST['vehicle_id'];
$query = sprintf("INSERT INTO v_images(photo, file_ype, vehicle_id, thumbnail) VALUES
('%s', '%s', '%s', '%s')", $photo, $_FILES['file']['type'], $vehicle_id, $thumbnail);

 

cameraman




msg:3749413
 9:28 am on Sep 22, 2008 (gmt 0)

imagecreatefromstring [us3.php.net] doesn't do quite what you think it does:

Create a new image from the image stream in the string

What you want is imagecreatefromgif or jpeg or png. You can use getimagesize() [us3.php.net] to figure out which one to use to create the image.

matthewamzn




msg:3749628
 4:07 pm on Sep 22, 2008 (gmt 0)

I found a function to handle the resizing for different image formats (I think it looks like it should work). But, I can't get the thumbnail image that it outputs into the database.

$photo = addslashes(fread(fopen($_FILES['file']['tmp_name'], "r"),$_FILES['file']['size']));

function img_resize($photo_name, $from, $max, $to) {
$ext = strtolower(end(explode('.',$photo_name)));
if ($ext == 'jpg' $ext == 'jpeg') {
$im = imagecreatefromjpeg($from);
} else if ($ext == 'gif') {
$im = imagecreatefromgif($from);
} else if ($ext == 'png') {
$im = imagecreatefrompng($from);
} else {
return false;
}
$sx = imagesx($im);
$sy = imagesy($im);
$nx = $sx;
$ny = $sy;
if ($nx > $max) {
$nx = $max;
$ny = max(1, round($ny * ($nx / $sx)));
}
if ($ny > $max) {
$ny = $max;
$nx = max(1, round($nx * ($ny / $sy)));
}
$nm = imagecreatetruecolor($nx, $ny);
imagealphablending ($nm, false);
imagecopyresampled ($nm, $im, 0, 0, 0, 0, $nx, $ny, $sx, $sy);
if ($ext == 'jpg' $ext == 'jpeg') {
imagejpeg ($nm, $to);
} else if ($ext == 'gif') {
imagegif ($nm, $to);
} else if ($ext == 'png') {
imagesavealpha ($nm, true);
imagepng ($nm, $to);
} else {
return false;
}
}

$thumbnail = img_resize($_FILES['file']['size'], $_FILES['file']['tmp_name'], 200, 'resized.jpg');

$vehicle_id = $_POST['vehicle_id'];

$query = sprintf("INSERT INTO v_images(photo, file_ype, vehicle_id, thumbnail) VALUES
('%s', '%s', '%s', '%s')", $photo, $_FILES['file']['type'], $vehicle_id, $thumbnail);

cameraman




msg:3749708
 6:04 pm on Sep 22, 2008 (gmt 0)

Your function returns false if the file extension doesn't match any of your ifs, but doesn't return anything otherwise. So
$thumbnail = img_resize(...
is going to be empty if everything went ok.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved