imagecopyresampled对图像采样压缩jpg正常,png采样后变成0字节,系统也无法打开
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;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
