0

I am using the following code for creating image thumbnail. It is working properly for JPG & JPEG images but not for PNG image thumbnail.

    $id = $_POST['mid'];
    $previousImage = $_POST["oldImage"];

    $imgSize = $_FILES['profile_image']['size'];
    //call thumbnail creation function and store thumbnail name
    $upload_img = cwUpload('profile_image',$mat_upload_dir,'',TRUE,$mat_upload_dir_thumb,'200','200');

    //full path of the thumbnail image
    $thumb_src = $mat_upload_dir_thumb.$upload_img;

    $query="update tbl_matrimony set image='".$upload_img."' where id='".$id."'";
    $res = mysql_query($query) or die(mysql_error());
    if ($res)
    {
        echo "Image Uploaded";
    }
    else
    {
        echo "Error occurred";
    }
}

function cwUpload($field_name = '', $target_folder = '', $file_name = '', $thumb = FALSE, $thumb_folder = '', $thumb_width = '', $thumb_height = ''){
    //folder path setup
    $target_path = $target_folder;
    $thumb_path = $thumb_folder;

    //file name setup
    $filename_err = explode(".",$_FILES[$field_name]['name']);
    $filename_err_count = count($filename_err);
    $file_ext = $filename_err[$filename_err_count-1];
    if($file_name != '')
    {
        $fileName = $file_name.'.'.$file_ext;
    }
    else
    {
        $fileName = $_FILES[$field_name]['name'];
    }

    //upload image path
    $upload_image = $target_path.basename($fileName);

    //upload image

    if(move_uploaded_file($_FILES[$field_name]['tmp_name'],$upload_image))
    {
        unlink($mat_upload_dir.$previousImage);
        //thumbnail creation
        if($thumb == TRUE)
        {
            $thumbnail = $thumb_path.$fileName;
            list($width,$height) = getimagesize($upload_image);
            $thumb_create = imagecreatetruecolor($thumb_width,$thumb_height);
            switch($file_ext){
                case 'jpg':
                    $source = imagecreatefromjpeg($upload_image);
                    break;
                case 'jpeg':
                    $source = imagecreatefromjpeg($upload_image);
                    break;
                case 'png':
                    $source = imagecreatefrompng($upload_image);
                    break;
                case 'gif':
                    $source = imagecreatefromgif($upload_image);
                    break;
                default:
                    $source = imagecreatefromjpeg($upload_image);
            }
            imagecopyresized($thumb_create,$source,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
            switch($file_ext){
                case 'jpg' || 'jpeg':
                    imagejpeg($thumb_create,$thumbnail,100);
                    break;
                case 'png':
                    imagepng($thumb_create,$thumbnail,100);
                    break;
                case 'gif':
                    imagegif($thumb_create,$thumbnail,100);
                    break;
                default:
                    imagejpeg($thumb_create,$thumbnail,100);
            }
        }

        return $fileName;
    }
    else
    {
        return false;
    }
}
?>
Pang
  • 9,564
  • 146
  • 81
  • 122
  • what is the error you are getting if any? What is the expected behaviour with `png` and what you are getting? – Vivek Kumar Feb 17 '17 at 06:43
  • i am getting black background of png image. – Ashish Odich Feb 17 '17 at 06:53
  • maybe the `imagecreatefrompng` is not working properly. Look at [its documentation](https://secure.php.net/manual/en/function.imagecreatefrompng.php) and also the comments below it. – Vivek Kumar Feb 17 '17 at 07:01
  • still not working. I cheacked **imagecreatefrompng** , – Ashish Odich Feb 17 '17 at 07:07
  • did you check the output of line `$source = imagecreatefrompng($upload_image);`? Is the $source black or correct as you want? – Vivek Kumar Feb 17 '17 at 08:44
  • yes i checked that – Ashish Odich Feb 17 '17 at 10:31
  • If you're writing new code, **_please_ don't use the `mysql_*` functions**. They are old and broken, were deprecated in PHP 5.5 (which is so old it no longer even receives security updates), and completely removed in PHP 7. Use [`PDO`](https://secure.php.net/manual/en/book.pdo.php) or [`mysqli_*`](https://secure.php.net/manual/en/book.mysqli.php) with _prepared statements_ and _parameter binding_ instead. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Feb 20 '17 at 21:20

1 Answers1

0

Try adding a case for uppercase PNG as well. Like so:

case 'PNG':
            $source = imagecreatefrompng($upload_image);
            break;

and

case 'png' || 'PNG':
            imagepng($thumb_create,$thumbnail,100);
            break;
Ashil John
  • 7,362
  • 4
  • 19
  • 34