[LEFT]Hi everyone,
Ive been breaking my head for the last couple of days with the following:
Im not a php guy but i managed to modify a php code that makes 3 images thumb, small and big images (used to do 2) from one “bigger” or smaller or landscape or vertical one, to fit my design.
My design is supposed to have: (widht and height respect.)
- the thumb 120 x 150 px max
- the small 450px by whatever (actually Ive setted 620, why i don know) height
- the big 800 px max (widht) by 900 max
So I have setted different parameters to meet this.
The problem Im getting is that sometimes some images that for me seem pretty “sqare” do not do what they are supposed to, so I dont know what the heck is going on here or maybe its right, its just a matter of math.
here is the code: (dont look at the comments, Ive been moving everything up down so lots are not what they say)
GODD LUCK and GUTS.
Of course if someone know something easier for a gallery that makes 3 images or even crops the thumb, will be great!!)
<?php
function FileUpload($name)
{
if($_FILES[$name][‘name’]!="")//Thumb Image Upload
{
$filename=$_FILES[$name][‘name’];
$idir = “…/images/gallery/pieces/big/”; // Path To Images Directory
$tdir = “…/images/gallery/pieces/thumbs/”; // Path To Thumbnails Directory
$sdir = “…/images/gallery/pieces/small/”; // Path To Small Directory
$twidth = “120”;
$theight = “150”;
$swidth = “450”;
$sheight = “626”;
$iwidth = “800”;
$iheight = “700”;
$url = $_FILES[$name][‘name’]; // Set $url To Equal The Filename For Later Use
if ($_FILES[$name][‘type’] == “image/jpg” || $_FILES[$name][‘type’] == “image/jpeg” || $_FILES[$name][‘type’] == “image/pjpeg”) {
$file_ext = strrchr($_FILES[$name][‘name’], ‘.’); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
$copy = copy($_FILES[$name][‘tmp_name’], “$idir” . $_FILES[$name][‘name’]); // Move Image From Temporary Location To Permanent
$simg = ImageCreateFromJPEG("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From
$currwidth = imagesx($simg); // Current Image Width
$currheight = imagesy($simg); // Current Image Height
if ($currheight > $currwidth) { // If Height Is Greater Than Width
$zoom = $theight / $currheight; // Length Ratio For Width
$newheight = $theight; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width
} else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $twidth / $currwidth; // Length Ratio For Height
$newwidth = $twidth; // Width Is Equal To Max Width
$newheight = $currheight * $zoom; // Creates The New Height
}
$dimg = imagecreatetruecolor($newwidth, $newheight); // Make New Image For Thumbnail
imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagejpeg($dimg, "$tdir" . $url); // Saving The Image
imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
//Small Image
$simg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The image From
$scurrwidth = imagesx($simg); // Current Image Width
$scurrheight = imagesy($simg); // Current Image Height
if ($scurrheight > $sheight) { // If Height Is Greater Than Width
$zoom = $sheight / $scurrheight; // Length Ratio For Width
$snewheight = $sheight; // Height Is Equal To Max Height
$snewwidth = $scurrwidth * $zoom; // Creates The New Width
} else if($scurrwidth > $swidth){ // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $swidth / $scurrwidth; // Length Ratio For Height
$snewwidth = $swidth; // Width Is Equal To Max Width
$snewheight = $scurrheight * $zoom; // Creates The New Height
} else {
$snewwidth = $scurrwidth;
$snewheight = $scurrheight;
}
$dimg = imagecreatetruecolor($snewwidth, $snewheight); // Make New Image For Thumbnail
imagecopyresized($dimg, $simg, 0, 0, 0, 0, $snewwidth, $snewheight, $scurrwidth, $scurrheight); // Copy Resized Image To The New Image (So We Can Save It)
imagejpeg($dimg, "$sdir" . $url); // Saving The Image
imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
//Big Image
$simg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From
$icurrwidth = imagesx($simg); // Current Image Width
$icurrheight = imagesy($simg); // Current Image Height
if ($icurrwidth > $iwidth) { // If Height Is Greater Than Width
$zoom = $iwidth / $icurrwidth; // Length Ratio For Width
$inewwidth = $iwidth; // Height Is Equal To Max Height
$inewheight = $icurrheight * $zoom; // Creates The New Width
} else if($icurrheight > $iheight){ // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $iheight / $icurrheight; // Length Ratio For Height
$inewheight = $iheight; // Width Is Equal To Max Width
$inewwidth = $icurrwidth * $zoom; // Creates The New Height
} else {
$inewwidth = $icurrwidth;
$inewheight = $icurrheight;
}
$dimg = imagecreatetruecolor($inewwidth, $inewheight); // Make New Image For Thumbnail
imagecopyresized($dimg, $simg, 0, 0, 0, 0, $inewwidth, $inewheight, $icurrwidth, $icurrheight); // Copy Resized Image To The New Image (So We Can Save It)
imagejpeg($dimg, "$idir" . $url); // Saving The Image
imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
}
} else {
$filename=’’;
}
return $filename;
}
?>
Thanks guys for your help,
[/LEFT]