How to resize a picture using php GD library.
This php code is used to create thumbnail of a jpg picture or just resize or crop a picture keeping original aspect ratios of picture. We have to enable GD library of PHP to resize picture with this PHP program.
Next is a function which we are using to resize the picture
function pic_resize($add,$user,$pic_name,$width_old,$height_old,$new_w_h,$new_add){
$old_x = $width_old;
$old_y = $height_old;
$new_w =$new_w_h;
$new_h =$new_w_h;
if ($old_x > $old_y)
{
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y)
{
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y)
{
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$n_width=$thumb_w; // Fix the width of the thumb nail images
$n_height=$thumb_h; // Fix the height of the thumb nail imaage
$tsrc=$new_add;// Path where thumb nail image will be stored
$im=ImageCreateFromJPEG($add);
$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
ImageJpeg($newimage,$tsrc);
chmod("$tsrc",0777);
}//end of function resize
Calling the resizing funcion
pic_resize($sorce_folder_pic,$user,$pic_name,$width_old,$height_old,$new_w_h, $des_folder_pic);