Hi there,
Sorry I am still a newb at PHP at would like someone to just have a look at my image resize function that I have built inside an uploading class using several tutorials and brain cells. Though the script does work after seeing more examples I wonder if I have over done this little project.
Basically the function needs to determine if the image passed to it is smaller than the parameters set it does nothing otherwise it will resize or rescale (probably easier if I get straight to the code!)
To call the funtion:
Then the functions the first will find the file extension to work with then the actual resize function:
I hope this makes some sense!
Thanks
Sorry I am still a newb at PHP at would like someone to just have a look at my image resize function that I have built inside an uploading class using several tutorials and brain cells. Though the script does work after seeing more examples I wonder if I have over done this little project.
Basically the function needs to determine if the image passed to it is smaller than the parameters set it does nothing otherwise it will resize or rescale (probably easier if I get straight to the code!)
To call the funtion:
Code:
$this->resize_image($this->path . $this->file["name"], 50, 50, null, true, 100, $this->path."s_".$this->file['raw_name'].$this->file["extention"]);
Then the functions the first will find the file extension to work with then the actual resize function:
Code:
function file_extension($filename){
$extension = substr( $filename, ( strrpos($filename, '.') + 1 ) ) ;
$extension = strtolower( $extension ) ;
return $extension;
}
function resize_image($image_name, $max_width = null, $max_height = null, $scale = null, $relscale = true, $quality = 100, $thumb_name){
$img = null;
$ext = $this->file_extension($image_name);
if ($ext == 'jpg' || $ext == 'jpeg') {
$img = @imagecreatefromjpeg($image_name);
} else if ($ext == 'png') {
$img = @imagecreatefrompng($image_name);
} else if ($ext == 'gif') {
$img = @imagecreatefromgif($image_name);
}
// If an image was successfully loaded, test the image for size
if ($img) {
// Get image size and scale ratio
list($oldwidth, $oldheight) = getimagesize($image_name);
if($max_width >= $oldwidth && $max_height >= $oldheight){ // This should stop upscaling
$width = $oldwidth;
$height = $oldheight;
} else {
if($relscale == true && ($max_width || $max_height)){ // Supply bounds, scale w/o loss to ratio
if($oldheight > $oldwidth || !$max_width){
$sizefactor = (double) ($max_height / $oldheight);
$width = (int) ($oldwidth * $sizefactor);
$height = (int) ($oldheight * $sizefactor);
}
else if($oldheight < $oldwidth || !$max_height){
$sizefactor = (double) ($max_width / $oldwidth) ;
$width = (int) ($oldwidth * $sizefactor);
$height = (int) ($oldheight * $sizefactor);
} else { // If the image has a ratio of 1, aka Height and Width ==, just do a generic resize
$width = $max_width;
$height = $max_height;
}
} elseif( $max_width && $max_height && $relscale == false ){ // Max Width And Height are new dimensions, just do a generic resize
$width = $max_width;
$height = $max_height;
} elseif( $scale && !$max_width || !$max_height ){ // Scale Provided And No Max Width/Height
$width = (int) ($oldwidth * ( $scale / 100 ));
$height = (int) ($oldheight * ( $scale / 100 ));
} else {
return false; // No Dimensions Specified BUT if this function were to be accidentally called...
}
}
$tmp_img = imagecreatetruecolor($width, $height);
// Copy and resize old image into new image
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $width, $height, $oldwidth, $oldheight);
imagedestroy($img);
$img = $tmp_img;
if($thumb_name == ""){
$thumb_name = $image_name;
}
if ($ext == 'png') {
imagepng($img,$thumb_name);
} elseif($ext == 'gif'){
imagegif($img,$thumb_name);
} else {
imagejpeg($img,$thumb_name,$quality);
}
}
}
I hope this makes some sense!
Thanks