/*
Function: Adjust image size or generate thumbnail Return: True/False Parameters: $Image The image that needs to be adjusted (including path) $Dw =450 The maximum width when adjusting; the absolute width when thumbnailing $Dh=450 The maximum height when adjusting; the absolute height when thumbnailing $Type=1 1, adjust the size; 2, generate thumbnails site http:// bbs.it-home.org */
$path='img/';//path
$phtypes=array(
'img/gif',
'img/jpg' ,
'img/jpeg',
'img/bmp',
'img/pjpeg',
'img/x-png'
);
FunctionImg($Image,$Dw =450,$Dh=450,$Type=1){
IF(!File_Exists($Image)){
ReturnFalse;
}
#If you need to generate a thumbnail, copy the original image and re-assign $Image
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]){
Case1:
$Img =@ImageCreateFromGIF($Image);
Break;
Case2:
$Img =@ImageCreateFromJPEG($Image);
Break;
Case3:
$Img =@ImageCreateFromPNG($Image);
Break;
}< ;/p>
#If the object is not created successfully, it means it is not an 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);}
ReturnFalse;
}
#If the resize operation is performed,
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 to the browser or file in JPEG format
ReturnTrue;
#If you are performing a thumbnail generation operation, then
}Else{
$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{ #Width Relatively large
$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);
ReturnTrue;
}
}
?>
Copy code
2. Call example
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
2024-10-22 09:46:29
2024-10-13 13:53:41
2024-10-12 12:15:51
2024-10-11 22:47:31
2024-10-11 19:36:51
2024-10-11 15:50:41
2024-10-11 15:07:41
2024-10-11 14:21:21
2024-10-11 12:59:11
2024-10-11 12:17:31