Yes, there's a way to do it. I wrote something that would let me upload a file and make a thumbnail of it that was either 60 pixels high or 60 pixels wide, depending on whether it was portrait or landscape.
here's a brief sample of how i did it:
1. in my form, i called the file field "img1." PHP has a bunch of built-in variables that it associates with an uploaded file, like its name and mime type, etc., which are all, thankfully, named things like $img1_name and $img1_type. you can read more about that on the php.net docs.
2. here's the php stuff. i hope it helps:
//create a full path to the newly uploaded file
$mypath = "/path/to/uploaded_file"
$working_file=("$mypath" . "$img1_name"
//write a tmp file using a different function depending on mime type
if ($img1_type=="image/jpeg" {
$src_img = imagecreatefromjpeg($working_file);
} elseif ($img1_type=="image/png" {
$src_img = imagecreatefrompng($working_file) or die("couldn't create image using imagecreatefrompng"
}
//if width is more than height, make width 60 pixels and
//make height proportional
if ($orig_width >= $orig_height) {
$new_w = 60;
$resize_factor = 60/$orig_width;
$new_h = $orig_height * $resize_factor;
} else {
//else use the height and make width proportional
$new_h = 60;
$resize_factor = 60/$orig_height;
$new_w = $orig_width * $resize_factor;
}
$dst_img = imagecreate($new_w,$new_h);
imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesX($src_img),imagesY($src_img));
//make a thumbnail using different functions based on mimetype of original
if ($img1_type=="image/jpeg" {
imagejpeg($dst_img, "../thumbnails/$img1_name"
} elseif ($img1_type=="image/png" {
imagepng($dst_img, "../thumbnails/$img1_name" or die("couldn't use imagepng"
}
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.