cozzy1984
02-14-2008, 06:43 PM
Hi, just found a useful script that im looking to modify for uploading and creating a thumbnail of an image.
$size = 80; // the thumbnail height
$filedir = 'pics/'; // the directory for the original image
$thumbdir = 'pics/'; // the directory for the thumbnail image
$prefix = 'small_'; // the prefix to be added to the original name
$maxfile = '2000000';
$mode = '0666';
$userfile_name = $_FILES['image']['name'];
$userfile_tmp = $_FILES['image']['tmp_name'];
$userfile_size = $_FILES['image']['size'];
$userfile_type = $_FILES['image']['type'];
if (isset($_FILES['image']['name']))
{
$prod_img = $filedir.$userfile_name;
$prod_img_thumb = $thumbdir.$prefix.$userfile_name;
move_uploaded_file($userfile_tmp, $prod_img);
chmod ($prod_img, octdec($mode));
$sizes = getimagesize($prod_img);
$aspect_ratio = $sizes[1]/$sizes[0];
if ($sizes[1] <= $size)
{
$new_width = $sizes[0];
$new_height = $sizes[1];
}else{
$new_height = $size;
$new_width = abs($new_height/$aspect_ratio);
}
$destimg=ImageCreateTrueColor($new_width,$new_height)
or $error['image'] = 'Problem in creating image'; $imageerror = 'regerrorwrong';
$srcimg=ImageCreateFromJPEG($prod_img)
or $error['image'] = 'Problem in opening source'; $imageerror = 'regerrorwrong';
if(function_exists('imagecopyresampled'))
{
imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg) )
or $error['image'] = 'Problem in resizing'; $imageerror = 'regerrorwrong';
}else{
Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
or $error['image'] = 'Problem in resizing'; $imageerror = 'regerrorwrong';
}
ImageJPEG($destimg,$prod_img_thumb,90)
or $error['image'] = 'Problem in saving'; $imageerror = 'regerrorwrong';
imagedestroy($destimg);
}
I have the code working now uploading the path of thumbnail and path of image to my databse, although now am looking to modify the creating thumbnail code a bit. I am looking basically to know if there is a simpler way of doing it than above, and also how to incororate gifs and maybe png files but produce error on other file types. At the moment if i try to upload for example a txt file it produces a lot of warning error messages:
Sorry Guys, managed to get this working, after all. Just had a brainwave i guess.
$size = 80; // the thumbnail height
$filedir = 'pics/'; // the directory for the original image
$thumbdir = 'pics/'; // the directory for the thumbnail image
$prefix = 'small_'; // the prefix to be added to the original name
$maxfile = '2000000';
$mode = '0666';
$userfile_name = $_FILES['image']['name'];
$userfile_tmp = $_FILES['image']['tmp_name'];
$userfile_size = $_FILES['image']['size'];
$userfile_type = $_FILES['image']['type'];
if (isset($_FILES['image']['name']))
{
$prod_img = $filedir.$userfile_name;
$prod_img_thumb = $thumbdir.$prefix.$userfile_name;
move_uploaded_file($userfile_tmp, $prod_img);
chmod ($prod_img, octdec($mode));
$sizes = getimagesize($prod_img);
$aspect_ratio = $sizes[1]/$sizes[0];
if ($sizes[1] <= $size)
{
$new_width = $sizes[0];
$new_height = $sizes[1];
}else{
$new_height = $size;
$new_width = abs($new_height/$aspect_ratio);
}
$destimg=ImageCreateTrueColor($new_width,$new_height)
or $error['image'] = 'Problem in creating image'; $imageerror = 'regerrorwrong';
$srcimg=ImageCreateFromJPEG($prod_img)
or $error['image'] = 'Problem in opening source'; $imageerror = 'regerrorwrong';
if(function_exists('imagecopyresampled'))
{
imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg) )
or $error['image'] = 'Problem in resizing'; $imageerror = 'regerrorwrong';
}else{
Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
or $error['image'] = 'Problem in resizing'; $imageerror = 'regerrorwrong';
}
ImageJPEG($destimg,$prod_img_thumb,90)
or $error['image'] = 'Problem in saving'; $imageerror = 'regerrorwrong';
imagedestroy($destimg);
}
I have the code working now uploading the path of thumbnail and path of image to my databse, although now am looking to modify the creating thumbnail code a bit. I am looking basically to know if there is a simpler way of doing it than above, and also how to incororate gifs and maybe png files but produce error on other file types. At the moment if i try to upload for example a txt file it produces a lot of warning error messages:
Sorry Guys, managed to get this working, after all. Just had a brainwave i guess.