imagecopyresampled對映像採樣壓縮jpg正常,png採樣後變成0位元組,系統也無法開啟

來源:互聯網
上載者:User
imagecopyresampled對映像採樣壓縮jpg正常,png採樣後變成0位元組,系統也無法開啟,代碼如下:

 $width = 200;$height = 200;header("Content-Type: {$data['file_type']}");           /// [file_type] => image/pnglist($width_orig, $height_orig) = getimagesize(".{$data['full_file_path']}");   //圖片路徑$ratio_orig = $width_orig/$height_orig;if ($width/$height > $ratio_orig) { $width = $height*$ratio_orig;} else {  $height = $width/$ratio_orig;}// 重新取樣$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'], 100);imagedestroy($image_p); imagedestroy($image);

jpg格式圖形採樣後正常 ,唯獨png 採樣壓縮有變成這樣:


回複討論(解決方案)

請貼全你的代碼

第14行格式png時應該用imagecreatefrompng()
你的程式沒做判斷?

第14行格式png時應該用imagecreatefrompng()
你的程式沒做判斷?


 

不好意思,代碼沒有上最新的:
//重新對圖片採樣,平滑插入像素值  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(1000, 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 = 200;$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 = $height*$ratio_orig;} else {   $height = $width/$ratio_orig;}// 重新取樣$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'], 100);imagedestroy($image_p);        imagedestroy($image);//exit;return $data;}else{return $CI->upload->display_errors();}}

請貼全你的代碼


//重新對圖片採樣,平滑插入像素值  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(1000, 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 = 200;$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 = $height*$ratio_orig;} else {   $height = $width/$ratio_orig;}// 重新取樣$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'], 100);imagedestroy($image_p);        imagedestroy($image);//exit;return $data;}else{return $CI->upload->display_errors();}}

找到原因了第40行
$push($image_p, $data['min_im'], 100);
第三個參數 函數為imagepng時範圍為0-9
imagejpeg時範圍為0-100

Regarding suggestions to rescale the 0-99 quality range of jpeg into the 0-9 range of png, note that for jpeg 99 is minimum compression (maximum quality) while for png 9 is maximum compression (quality doesn't change).

找到原因了第40行
$push($image_p, $data['min_im'], 100);
第三個參數 函數為imagepng時範圍為0-9
imagejpeg時範圍為0-100



謝謝您,這個問題已經解決了,請問能在儲存壓縮好的圖片後不在瀏覽器輸出嗎?我只想直接返回數組,但卻總是在瀏覽器裡產生一個20X20的圖形並停止運行下去
// 重新取樣$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'], 100);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;
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.