php生成图片缩略图方法收集
php生成图片缩略图方法函数代码如下:
<p><?php</p>/*<br />*php生成图片缩略图的函数<br />*www.scutephp.com<br />*/<br />function thumbImage($file,$thumbWidth,$thumbHeight,$savePath='',$isCut=true,$quality=100){<br /> $result=array('status'=>0,'file'=>'','width'=>0,'height'=>0,'savePath'=>'','info'=>'');<br /> if(!file_exists($file)){<br /> return array('status'=>1,'file'=>'','width'=>0,'height'=>0,'savePath'=>'','info'=>'图片文件不存在');<br /> }<br /> //检测文件类型<br /> $fp=fopen($file,'rb');<br /> $str=fread($fp,2); //只读2字节<br /> fclose($fp);<br /> $str=@unpack("c2chars",$str);<br /> $typeCode=intval($str['chars1'].$str['chars2']);<br /> $fileType='';<br /> switch($typeCode){<br /> case 255216:<br /> $fileType='jpg';<br /> break;<br /> case 7173:<br /> $fileType='gif';<br /> break;<br /> case 6677:<br /> $fileType='bmp';<br /> break;<br /> case 13780:<br /> $fileType='png';<br /> break;<br /> default:<br /> $fileType=$typeCode;<br /> break;<br /> }<br /> if($str['chars1']=='-1'&&$str['chars2']=='-40'){<br /> $fileType='jpg';<br /> }<br /> if($str['chars1']=='-119'&&$str['chars2']=='80'){<br /> $fileType='png';<br /> }<br /> if(!in_array($fileType,array('jpg','gif','bmp','png'))){<br /> return array('status'=>2,'file'=>'','width'=>0,'height'=>0,'savePath'=>'','info'=>'图片文件类型不正确:'.$fileType);<br /> }<br /> //图片缩放处理<br /> if($fileType=='jpg'){<br /> $im=imagecreatefromjpeg($file);<br /> }<br /> if($fileType=='gif'){<br /> $im=imagecreatefromgif($file);<br /> }<br /> if($fileType=='png'){<br /> $im=imagecreatefrompng($file);<br /> }<br /> if(empty($savePath))<br /> $savePath=$file;<br /> $width=imagesx($im);<br /> $height=imagesy($im);<br /> if($width<$thumbWidth&&$height<$thumbHeight){<br /> return array('status'=>3,'file'=>'','width'=>0,'height'=>0,'savePath'=>'','info'=>'图片尺寸小于生成缩略图的尺寸');<br /> }<br /> $ratio=$width/$height;//实际图象的比例<br /> $thumbRatio=$thumbWidth/$thumbHeight;//改变后的图象的比例<br /> if($isCut){//裁切处理<br /> if(function_exists('imagepng')&&(str_replace('.','',PHP_VERSION)>=512)){//针对php版本大于5.12参数变化后的处理情况<br /> $quality=9;<br /> }<br /> if($ratio>=$thumbRatio){//高度优先<br /> $newimg=imagecreatetruecolor($thumbWidth,$thumbHeight);<br /> imagecopyresampled($newimg,$im,0,0,0,0,$thumbWidth,$thumbHeight,(($height)*$thumbRatio),$height);<br /> imagejpeg($newimg,$savePath,$quality);<br /> }<br /> if($ratio<$thumbRatio){//宽度优先<br /> $newimg=imagecreatetruecolor($thumbWidth,$thumbHeight);<br /> imagecopyresampled($newimg,$im,0,0,0,0,$thumbWidth,$thumbHeight,$width,(($width)/$thumbRatio));<br /> imagejpeg($newimg,$savePath,$quality);<br /> }<br /> }else{//不裁切处理<br /> if($ratio>=$thumbRatio){<br /> $newimg=imagecreatetruecolor($thumbWidth,($thumbWidth)/$ratio);<br /> imagecopyresampled($newimg,$im,0,0,0,0,$thumbWidth,($thumbWidth)/$ratio,$width,$height);<br /> imagejpeg($newimg,$savePath,$quality);<br /> }<br /> if($ratio<$thumbRatio){<br /> $newimg=imagecreatetruecolor(($thumbHeight)*$ratio,$thumbHeight);<br /> imagecopyresampled($newimg,$im,0,0,0,0,($thumbHeight)*$ratio,$thumbHeight,$width,$height);<br /> imagejpeg($newimg,$savePath,$quality);<br /> }<br /> }<br /> ImageDestroy($im);//www.scutephp.com/php-function/1007.html<br /> return array('status'=>0,'file'=>$file,'width'=>$thumbWidth,'height'=>$thumbHeight,'savePath'=>$savePath,'info'=>'缩略图片生成成功');<br /><p>}
使用方法如下:
$a=thumbImage('1.jpg',200,100,'2.jpg');//将1.jpg生成宽为200,高为100的缩略图,并保存为2.jpg<br />print_r($a);
打印结果:
Array(
[status] => 0
[file] => 1.jpg
[width] => 200
[height] => 200
[savePath] => 2.jpg
[info] => 缩略图片生成成功
)
$a=thumbImage('1.jpg',200,100);//将1.jpg生成宽为200,高为100的缩略图,并直接覆盖1.jpg,不生成新图片

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.
