phpfunctionimage-resizinggdlib

GD LIB how to convert every uploaded images successfully into jpg type?


please help me out with my current function to make it convert any other image types successfully into a proper jpg image type.

here is my function

    function resize_image($oldimage_name, $new_image_name){
    list($owidth,$oheight) = getimagesize($oldimage_name);
    $width = 250; $height = 250;    
    $im = imagecreatetruecolor($width, $height);
    $img_src = imagecreatefromjpeg($oldimage_name);
    imagealphablending($img_src, false);
    imagesavelalpha($img_src, true);
    imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
    imagejpeg($im, $new_image_name, 90);
    imagedestroy($im);
    unlink($oldimage_name);
    return true;
}

Thanking you for your time and help in advance


Solution

  • If it is not jpg you will fail. Use imagecreatefromstring() instead. This can guess the real format.

    Replace the code like this

    $img_src = imagecreatefromstring(file_get_contents($oldimage_name));