Hi guys,
Spent the past couple of hours tinkering about with this function i got off the php docs.
The code works to a point. Am hoping someone can help me with this final step.
The main image that is resized appears to skew and distort.
What i need is if it does not meet max height or max width then either fill to max height and max or width or crop to.
i also need the same to happen to the thumbnail created.
/**********************************************************
* function resizejpeg:
*
* = creates a resized image based on the max width
* specified as well as generates a thumbnail from
* a rectangle cut from the middle of the image.
*
* @dir = directory image is stored in
* @img = the image name
* @max_w = the max width of the resized image
* @max_h = the max height of the resized image
* @th_w = the width of the thumbnail
* @th_h = the height of the thumbnail
*
**********************************************************/
function resizejpeg($dir, $img, $max_w, $max_h, $th_w, $th_h){
// get original images width and height
list($or_w, $or_h, $or_t) = getimagesize($dir.$img);
// make sure image is a jpeg
if ($or_t == 2) {
// obtain the image's ratio
$ratio = ($or_h / $or_w);
// original image
$or_image = imagecreatefromjpeg($dir.$img);
// resize image
if ($or_w > $max_w || $or_h > $max_h) {
// first resize by width (less than $max_w)
if ($or_w > $max_w) {
$rs_w = $max_w;
$rs_h = $ratio * $max_h;
} else {
$rs_w = $or_w;
$rs_h = $or_h;
}
// then resize by height (less than $max_h)
if ($rs_h > $max_h) {
$rs_w = $max_w / $ratio;
$rs_h = $max_h;
}
// copy old image to new image
$rs_image = imagecreatetruecolor($rs_w, $rs_h);
imagecopyresampled($rs_image, $or_image, 0, 0, 0, 0, $rs_w, $rs_h, $or_w, $or_h);
} else {
$rs_w = $or_w;
$rs_h = $or_h;
$rs_image = $or_image;
}
// generate resized image
imagejpeg($rs_image, $dir.$img, 100);
$th_image = imagecreatetruecolor($th_w, $th_h);
// cut out a rectangle from the resized image and store in thumbnail
$new_w = (($rs_w /4));
$new_h = (($rs_h /4));
imagecopyresized($th_image, $rs_image, 0, 0, $new_w, $new_h, $rs_w, $rs_h, $rs_w, $rs_h);
$theName=substr($img, -10,1);
// generate thumbnail
imagejpeg($th_image, $dir.$theName.'_thumb.jpg', 100);
return true;
}
// Image type was not jpeg!
else {
return false;
}
}
$img = $k."_main.jpg";
resizejpeg($dir, $img, 360,540 , 60, 90);