imagecopyresampled image sample Compression jpg Normal, PNG sample into 0 bytes, the system will not open

Source: Internet
Author: User
Tags imagejpeg
imagecopyresampled image sampling Compression jpg is normal, PNG sample becomes 0 bytes, the system can not be opened, the code is as follows:

$width = N, $height = 200;header ("Content-type: {$data [' File_type ']}");           [File_type] = image/pnglist ($width _orig, $height _orig) = GetImageSize (". { $data [' Full_file_path ']} ");   Picture path $ratio_orig = $width _orig/$height _orig;if ($width/$height > $ratio _orig) {$width = $height * $ratio _orig;} else {  $height = $width/$ratio _orig;} resampling $image_p = Imagecreatetruecolor ($width, $height); $image = Imagecreatefromjpeg (". { $data [' Full_file_path '} '); $push = "image". $data [' Image_type '];imagecopyresampled ($image _p, $image, 0, 0, 0, 0, $width , $height, $width _orig, $height _orig), $push ($image _p, $data [' Min_im '], Imagedestroy ($image _p); Imagedestroy ($image);

JPG format graphics are normal after sampling, but PNG sampling compression has changed to this:


Reply to discussion (solution)

Please post all your code.

The 14th row format PNG should use Imagecreatefrompng ()
Your program didn't make a judgment?

The 14th row format PNG should use Imagecreatefrompng ()
Your program didn't make a judgment?



Sorry, the code is not up to date:
Resample picture, smooth insert pixel value function upload_headimg ($file = ' file ') {$path = GetPath (); if (empty ($path)) {return ';} $config [' upload_path '] = $path; $config [' allowed_types '] = ' jpg|jpeg|gif|png '; $config [' max_size '] = ' 10240 '; $config [' file_name '] = ' wj_ '. Time (). Mt_rand (+ 9999); $config [' file_ext_tolower '] = TRUE; $CI = &get_instance (); $CI Load->library (' upload ', $config), $res = $CI->upload->do_upload ($file), if ($res) {$data = $CI->upload-> Data (); $path = LTrim ($path, '. '); $data [' full_file_path '] = $path. ' /'. $data [' file_name ']; $data [' min_im '] = '. '. $path. ' /min '. $data [' file_name ']; $width = €; $height = 200;header ("Content-type: {$data [' File_type ']}"); List ($width _orig, $ Height_orig) = GetImageSize (". { $data [' Full_file_path ']} "$ratio _orig = $width _orig/$height _orig;if ($width/$height > $ratio _orig) {$width = $heig ht* $ratio _orig;} else {$height = $width/$ratio _orig;} resampling $image_p = Imagecreatetruecolor ($width, $height); $create _im = "Imagecreatefrom". $data [' Image_type ']; $iMage = $create _im (". { $data [' Full_file_path '} '); $push = "image". $data [' Image_type '];imagecopyresampled ($image _p, $image, 0, 0, 0, 0, $width        , $height, $width _orig, $height _orig), $push ($image _p, $data [' Min_im '], Imagedestroy ($image _p); Imagedestroy ($image);//exit;return $data;} Else{return $CI->upload->display_errors ();}}

Please post all your code.


Resample picture, smooth insert pixel value function upload_headimg ($file = ' file ') {$path = GetPath (); if (empty ($path)) {return ';} $config [' upload_path '] = $path; $config [' allowed_types '] = ' jpg|jpeg|gif|png '; $config [' max_size '] = ' 10240 '; $config [' file_name '] = ' wj_ '. Time (). Mt_rand (+ 9999); $config [' file_ext_tolower '] = TRUE; $CI = &get_instance (); $CI Load->library (' upload ', $config), $res = $CI->upload->do_upload ($file), if ($res) {$data = $CI->upload-> Data (); $path = LTrim ($path, '. '); $data [' full_file_path '] = $path. ' /'. $data [' file_name ']; $data [' min_im '] = '. '. $path. ' /min '. $data [' file_name ']; $width = €; $height = 200;header ("Content-type: {$data [' File_type ']}"); List ($width _orig, $ Height_orig) = GetImageSize (". { $data [' Full_file_path ']} "$ratio _orig = $width _orig/$height _orig;if ($width/$height > $ratio _orig) {$width = $heig ht* $ratio _orig;} else {$height = $width/$ratio _orig;} resampling $image_p = Imagecreatetruecolor ($width, $height); $create _im = "Imagecreatefrom". $data [' Image_type ']; $iMage = $create _im (". { $data [' Full_file_path '} '); $push = "image". $data [' Image_type '];imagecopyresampled ($image _p, $image, 0, 0, 0, 0, $width        , $height, $width _orig, $height _orig), $push ($image _p, $data [' Min_im '], Imagedestroy ($image _p); Imagedestroy ($image);//exit;return $data;} Else{return $CI->upload->display_errors ();}}

I found the reason. Line 40th
$push ($image _p, $data [' min_im '], 100);
The third parameter function is imagepng with a range of 0-9
Imagejpeg Time range of 0-100

Regarding suggestions to rescale the 0-99 quality range of JPEG into the 0-9 range of PNG, note this for JPEG are Minimu M compression (maximum quality) while for PNG 9 are maximum compression (quality doesn ' t change).

I found the reason. Line 40th
$push ($image _p, $data [' min_im '], 100);
The third parameter function is imagepng with a range of 0-9
Imagejpeg Time range of 0-100



Thank you, this problem has been solved, can I save the compressed picture after the browser output? I just want to return the array directly, but I always generate a 20x20 graphic in my browser and stop running.
resampling $image_p = Imagecreatetruecolor ($width, $height); $create _im = "Imagecreatefrom". $data [' Image_type ']; $image = $ Create_im (". { $data [' Full_file_path '} '); $push = "image". $data [' Image_type '];imagecopyresampled ($image _p, $image, 0, 0, 0, 0, $width , $height, $width _orig, $height _orig), switch ($data [' Image_type ']) {case ' jpeg ': $status = $push ($image _p, $data [' Min_ Im '], break;case ' png ': $status = $push ($image _p, $data [' Min_im '], 9); Break;default: $status = $push ($image _p, $data [' Min_im ']); break;} Imagedestroy ($image _p);        Imagedestroy ($image); return $data;
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.