支持png透明图片的php生成缩略图类分享,
注:此功能依赖GD2图形库
最近要用php生成缩略图,在网上找了一下,发现了这篇文章:PHP生成图片缩略图
试用了一下后,发现有这样几个问题:
1、png图片生成的缩略图是jpg格式的
2、png图片生成的缩略图没有了透明(半透明)效果(填充了黑色背景)
3、代码语法比较老
因此,在这个版本的基础上简单修改优化了一下。
PHP生成缩略图类
<?php /* * desc: Resize Image(png, jpg, gif) * author: 十年后的卢哥哥 * date: 2014.11.13 */ class ResizeImage { //图片类型 private $type; //实际宽度 private $width; //实际高度 private $height; //改变后的宽度 private $resize_width; //改变后的高度 private $resize_height; //是否裁图 private $cut; //源图象 private $srcimg; //目标图象地址 private $dstimg; //临时创建的图象 private $im; function __construct($imgPath, $width, $height, $isCut, $savePath) { $this->srcimg = $imgPath; $this->resize_width = $width; $this->resize_height = $height; $this->cut = $isCut; //图片的类型 $this->type = strtolower(substr(strrchr($this->srcimg,"."),1)); //初始化图象 $this->initi_img(); //目标图象地址 $this -> dst_img($savePath); //-- $this->width = imagesx($this->im); $this->height = imagesy($this->im); //生成图象 $this->newimg(); ImageDestroy ($this->im); } private function newimg() { //改变后的图象的比例 $resize_ratio = ($this->resize_width)/($this->resize_height); //实际图象的比例 $ratio = ($this->width)/($this->height); if($this->cut) { //裁图 $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); if($this->type=="png") { imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127)); } if($ratio>=$resize_ratio) { //高度优先 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height); } else { //宽度优先 imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio)); } } else { //不裁图 if($ratio>=$resize_ratio) { $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio); if($this->type=="png") { imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127)); } imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height); } else { $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height); if($this->type=="png") { imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127)); } imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height); } } if($this->type=="png") { imagesavealpha($newimg, true); imagepng ($newimg,$this->dstimg); } else { imagejpeg ($newimg,$this->dstimg); } } //初始化图象 private function initi_img() { if($this->type=="jpg") { $this->im = imagecreatefromjpeg($this->srcimg); } if($this->type=="gif") { $this->im = imagecreatefromgif($this->srcimg); } if($this->type=="png") { $this->im = imagecreatefrompng($this->srcimg); } } //图象目标地址 private function dst_img($dstpath) { $full_length = strlen($this->srcimg); $type_length = strlen($this->type); $name_length = $full_length-$type_length; $name = substr($this->srcimg,0,$name_length-1); $this->dstimg = $dstpath; } } ?>
使用
使用时,直接调用类的构造函数即可,构造函数如下:
$resizeimage = new resizeimage($imgPath, $width, $height, $isCut, $savePath);
参数
$imgPath:原图片地址
$width:缩略图宽
$height:缩略图高
$isCut:是否裁剪,bool值
$savePath:缩略图地址(可以跟原图片地址相同)
示例
<?php include "ResizeImage.php"; //jpg $jpgResize = new ResizeImage("img/test_1920_1200.jpg", 320, 240, false, "img/test_320_240.jpg"); //png $pngResize = new ResizeImage("img/test_1024_746.png", 320, 240, false, "img/test_320_240.png"); ?>
效果

phpsession 실패 이유에는 구성 오류, 쿠키 문제 및 세션 만료가 포함됩니다. 1. 구성 오류 : 올바른 세션을 확인하고 설정합니다. 2. 쿠키 문제 : 쿠키가 올바르게 설정되어 있는지 확인하십시오. 3. 세션 만료 : 세션 시간을 연장하기 위해 세션을 조정합니다 .GC_MAXLIFETIME 값을 조정하십시오.

PHP에서 세션 문제를 디버그하는 방법 : 1. 세션이 올바르게 시작되었는지 확인하십시오. 2. 세션 ID의 전달을 확인하십시오. 3. 세션 데이터의 저장 및 읽기를 확인하십시오. 4. 서버 구성을 확인하십시오. 세션 ID 및 데이터를 출력, 세션 파일 컨텐츠보기 등을 통해 세션 관련 문제를 효과적으로 진단하고 해결할 수 있습니다.

Session_Start ()로 여러 통화를하면 경고 메시지와 가능한 데이터 덮어 쓰기가 발생합니다. 1) PHP는 세션이 시작되었다는 경고를 발행합니다. 2) 세션 데이터의 예상치 못한 덮어 쓰기를 유발할 수 있습니다. 3) Session_status ()를 사용하여 반복 통화를 피하기 위해 세션 상태를 확인하십시오.

SESSION.GC_MAXLIFETIME 및 SESSION.COOKIE_LIFETIME을 설정하여 PHP에서 세션 수명을 구성 할 수 있습니다. 1) SESSION.GC_MAXLIFETIME 서버 측 세션 데이터의 생존 시간을 제어합니다. 2) 세션 .Cookie_Lifetime 클라이언트 쿠키의 수명주기를 제어합니다. 0으로 설정하면 브라우저가 닫히면 쿠키가 만료됩니다.

데이터베이스 스토리지 세션 사용의 주요 장점에는 지속성, 확장 성 및 보안이 포함됩니다. 1. 지속성 : 서버가 다시 시작 되더라도 세션 데이터는 변경되지 않아도됩니다. 2. 확장 성 : 분산 시스템에 적용하여 세션 데이터가 여러 서버간에 동기화되도록합니다. 3. 보안 : 데이터베이스는 민감한 정보를 보호하기 위해 암호화 된 스토리지를 제공합니다.

SessionHandlerInterface 인터페이스를 구현하여 PHP에서 사용자 정의 세션 처리 구현을 수행 할 수 있습니다. 특정 단계에는 다음이 포함됩니다. 1) CustomsessionHandler와 같은 SessionHandlerInterface를 구현하는 클래스 만들기; 2) 인터페이스의 방법 (예 : Open, Close, Read, Write, Despare, GC)의 수명주기 및 세션 데이터의 저장 방법을 정의하기 위해 방법을 다시 작성합니다. 3) PHP 스크립트에 사용자 정의 세션 프로세서를 등록하고 세션을 시작하십시오. 이를 통해 MySQL 및 Redis와 같은 미디어에 데이터를 저장하여 성능, 보안 및 확장 성을 향상시킬 수 있습니다.

SessionId는 웹 애플리케이션에 사용되는 메커니즘으로 사용자 세션 상태를 추적합니다. 1. 사용자와 서버 간의 여러 상호 작용 중에 사용자의 신원 정보를 유지하는 데 사용되는 무작위로 생성 된 문자열입니다. 2. 서버는 쿠키 또는 URL 매개 변수를 통해 클라이언트로 생성하여 보낸다. 3. 생성은 일반적으로 임의의 알고리즘을 사용하여 독창성과 예측 불가능 성을 보장합니다. 4. 실제 개발에서 Redis와 같은 메모리 내 데이터베이스를 사용하여 세션 데이터를 저장하여 성능 및 보안을 향상시킬 수 있습니다.

JWT 또는 쿠키를 사용하여 API와 같은 무국적 환경에서 세션을 관리 할 수 있습니다. 1. JWT는 무국적자 및 확장 성에 적합하지만 빅 데이터와 관련하여 크기가 크다. 2. 쿠키는보다 전통적이고 구현하기 쉽지만 보안을 보장하기 위해주의해서 구성해야합니다.


핫 AI 도구

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

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

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

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

SublimeText3 영어 버전
권장 사항: Win 버전, 코드 프롬프트 지원!

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경

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

Eclipse용 SAP NetWeaver 서버 어댑터
Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

Atom Editor Mac 버전 다운로드
가장 인기 있는 오픈 소스 편집기
