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;

PHP는 현대적인 프로그래밍, 특히 웹 개발 분야에서 강력하고 널리 사용되는 도구로 남아 있습니다. 1) PHP는 사용하기 쉽고 데이터베이스와 완벽하게 통합되며 많은 개발자에게 가장 먼저 선택됩니다. 2) 동적 컨텐츠 생성 및 객체 지향 프로그래밍을 지원하여 웹 사이트를 신속하게 작성하고 유지 관리하는 데 적합합니다. 3) 데이터베이스 쿼리를 캐싱하고 최적화함으로써 PHP의 성능을 향상시킬 수 있으며, 광범위한 커뮤니티와 풍부한 생태계는 오늘날의 기술 스택에 여전히 중요합니다.

PHP에서는 약한 참조가 약한 회의 클래스를 통해 구현되며 쓰레기 수집가가 물체를 되 찾는 것을 방해하지 않습니다. 약한 참조는 캐싱 시스템 및 이벤트 리스너와 같은 시나리오에 적합합니다. 물체의 생존을 보장 할 수 없으며 쓰레기 수집이 지연 될 수 있음에 주목해야합니다.

\ _ \ _ 호출 메소드를 사용하면 객체를 함수처럼 호출 할 수 있습니다. 1. 객체를 호출 할 수 있도록 메소드를 호출하는 \ _ \ _ 정의하십시오. 2. $ obj (...) 구문을 사용할 때 PHP는 \ _ \ _ invoke 메소드를 실행합니다. 3. 로깅 및 계산기, 코드 유연성 및 가독성 향상과 같은 시나리오에 적합합니다.

섬유는 PHP8.1에 도입되어 동시 처리 기능을 향상시켰다. 1) 섬유는 코 루틴과 유사한 가벼운 동시성 모델입니다. 2) 개발자는 작업의 실행 흐름을 수동으로 제어 할 수 있으며 I/O 집약적 작업을 처리하는 데 적합합니다. 3) 섬유를 사용하면보다 효율적이고 반응이 좋은 코드를 작성할 수 있습니다.

PHP 커뮤니티는 개발자 성장을 돕기 위해 풍부한 자원과 지원을 제공합니다. 1) 자료에는 공식 문서, 튜토리얼, 블로그 및 Laravel 및 Symfony와 같은 오픈 소스 프로젝트가 포함됩니다. 2) 지원은 StackoverFlow, Reddit 및 Slack 채널을 통해 얻을 수 있습니다. 3) RFC에 따라 개발 동향을 배울 수 있습니다. 4) 적극적인 참여, 코드에 대한 기여 및 학습 공유를 통해 커뮤니티에 통합 될 수 있습니다.

PHP와 Python은 각각 고유 한 장점이 있으며 선택은 프로젝트 요구 사항을 기반으로해야합니다. 1.PHP는 간단한 구문과 높은 실행 효율로 웹 개발에 적합합니다. 2. Python은 간결한 구문 및 풍부한 라이브러리를 갖춘 데이터 과학 및 기계 학습에 적합합니다.

PHP는 죽지 않고 끊임없이 적응하고 진화합니다. 1) PHP는 1994 년부터 새로운 기술 트렌드에 적응하기 위해 여러 버전 반복을 겪었습니다. 2) 현재 전자 상거래, 컨텐츠 관리 시스템 및 기타 분야에서 널리 사용됩니다. 3) PHP8은 성능과 현대화를 개선하기 위해 JIT 컴파일러 및 기타 기능을 소개합니다. 4) Opcache를 사용하고 PSR-12 표준을 따라 성능 및 코드 품질을 최적화하십시오.

PHP의 미래는 새로운 기술 트렌드에 적응하고 혁신적인 기능을 도입함으로써 달성 될 것입니다. 1) 클라우드 컴퓨팅, 컨테이너화 및 마이크로 서비스 아키텍처에 적응, Docker 및 Kubernetes 지원; 2) 성능 및 데이터 처리 효율을 향상시키기 위해 JIT 컴파일러 및 열거 유형을 도입합니다. 3) 지속적으로 성능을 최적화하고 모범 사례를 홍보합니다.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

에디트플러스 중국어 크랙 버전
작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

SublimeText3 Linux 새 버전
SublimeText3 Linux 최신 버전
