Welcome to WebmasterWorld Guest from 54.226.60.235

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Upload image and create thumbnail

Uploading images to mysql database. Also creating thumbnails for the images

     

matthewamzn

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

10+ Year Member



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

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

WebmasterWorld Senior Member 5+ Year Member



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

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

10+ Year Member



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

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

WebmasterWorld Senior Member 5+ Year Member



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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month