-
-
/** - *
- *Function: Adjust image size or generate thumbnail
- *Return: True/False
- *Parameters:
- * $Image The image that needs to be adjusted (including path)
- * $Dw=450 Maximum width when adjusting; when thumbnailing The absolute width
- * $Dh=450 The maximum height when adjusting; the absolute height when thumbnailing
- * $Type=1 1, adjust the size; 2, generate thumbnails
- */ bbs.it-home.org
- $phtypes=array('img/gif', 'img/jpg', 'img /jpeg', 'img/bmp', 'img/pjpeg', 'img/x-png');
function compressImg($Image,$Dw,$Dh,$Type) {
- IF(!file_exists($Image)){
- return false;
- }
- // If you need to generate a thumbnail, copy the original image and reassign it to $Image (generate thumbnail operation)
- // When Type= =1, the original image file will not be copied, but the reduced image will be regenerated on the original image file (resize operation)
- IF($Type!=1){
- copy($Image,str_replace(" .","_x.",$Image));
- $Image=str_replace(".","_x.",$Image);
- }
- // Get the file type and create different objects according to different types
- $ImgInfo=getimagesize($Image);
- Switch($ImgInfo[2]){
- case 1:
- $Img =@imagecreatefromgif($Image);
- break;
- case 2:
- $Img =@imagecreatefromjpeg($ Image);
- Break;
- case 3:
- $Img =@imagecreatefrompng($Image);
- break;
- }
- // If the object is not created successfully, it means it is a non-image file
- IF(Empty($Img)){
- // If there is an error when generating thumbnails, you need to delete the copied files
- IF($Type!=1){
- unlink($Image);
- }
- return false;
- }
- // If yes To perform the resize operation,
- IF($Type==1){
- $w=ImagesX($Img);
- $h=ImagesY($Img);
- $width = $w;
- $height = $h;
- IF($width>$Dw){
- $Par=$Dw/$width;
- $width=$Dw;
- $height=$height*$Par;
- IF($height>$Dh){
- $Par= $Dh/$height;
- $height=$Dh;
- $width=$width*$Par;
- }
- } ElseIF($height>$Dh) {
- $Par=$Dh/$height;
- $height= $Dh;
- $width=$width*$Par;
- IF($width>$Dw){
- $Par=$Dw/$width;
- $width=$Dw;
- $height=$height*$Par;
- }
- } Else {
- $width=$width;
- $height=$height;
- }
- $nImg =ImageCreateTrueColor($width,$height);// Create a new true color canvas
- ImageCopyReSampled($nImg,$Img ,0,0,0,0,$width,$height,$w,$h);//Resample copy part of the image and resize it
- ImageJpeg($nImg,$Image);//Output the image in JPEG format To the browser or file
- return true;
- } Else {// If the thumbnail generation operation is performed,
- $w=ImagesX($Img);
- $h=ImagesY($Img);
- $width = $w;
- $height = $h;
- $nImg =ImageCreateTrueColor($Dw,$Dh);
- IF($h/$w>$Dh/$Dw){// Height is larger
- $width=$Dw;
- $ height=$h*$Dw/$w;
- $IntNH=$height-$Dh;
- ImageCopyReSampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w , $h);
- } Else {// Larger width ratio
- $height=$Dh;
- $width=$w*$Dh/$h;
- $IntNW=$width-$Dw;
- ImageCopyReSampled($nImg, $Img,-$IntNW/1.8,0,0,0, $width, $Dh, $w, $h);
- }
- ImageJpeg($nImg,$Image);
- return true;
- }
- };
-
- /**
- *Get the image on the server based on url
- *$url image path on the server $filename file name
- */
- function GrabImage($url,$filename="") {
- if($url=="") return false;
- if($filename=="") {
- $ext =strrchr($url,".");
- if($ext!=".gif" && $ext!=".jpg" && $ext!=".png")
- return false;
- $filename=date ("YmdHis").$ext;
- }
- ob_start();
- readfile($url);
- $img = ob_get_contents();
- ob_end_clean();
- $size = strlen($img);
-
- $fp2 =@fopen($filename, "a");
- fwrite($fp2,$img);
- fclose($fp2);
- return $filename;
- }
- ?>
-
Copy Code
Call method:
-
- //Network image path
- $imgPath = 'http://news.xxxx.cn/images/1382088444437.jpg';//Remote URL address
- $tempPath = 'aa/ bbs.jpg';//Save image path
-
- if(is_file($tempPath)){
- unlink($tempPath);
- }
-
- $bigImg=GrabImage($imgPath, $tempPath);
- compressImg($bigImg, 70,70,1);
- ?>
Copy code
|