-
- /**
- *
- *Function: adjust image size or generate thumbnail
- *Modified: 2013-2-15
- *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
- */
- $phtypes=array('img/gif', 'img/jpg', 'img/jpeg', 'img/bmp', 'img /pjpeg', 'img/x-png');
-
- function compressImg($Image,$Dw,$Dh,$Type){
- echo $Image;
- IF(!file_exists($Image)){
- echo " No image exists";
- return false;
- }
- echo "Image exists";
- // If you need to generate a thumbnail, copy the original image and re-assign 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 and 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;
- }
- };
- ?>
Copy code
2. Get remote pictures
-
- //Network image path
- $imgPath = 'http://bbs.it-home.org/phone/compress-img/251139474ba926db3d7850.jpg';
- //$imgPath = "http://bbs.it-home.org/userfiles/image/20111125/251139474ba926db3d7850.jpg";
- $tempPath = str_replace('http://bbs.it-home.org/', '', $imgPath );//Replace newline characters
- $name = strrchr($tempPath, "/");
- $path = str_replace($name, '', $tempPath);//Replace newline characters
-
- /**
- *Create multi-level directories based on the path path
- *$dir target directory $mode permissions, 0700 represents the highest permissions
- */
- function makedir( $dir , $mode = "0700" ) {
- if(strpos($dir , "/" )){
- $dir_path = "" ;
- $dir_info = explode("/" , $dir );
- foreach($dir_info as $key => $value){
- $dir_path .= $value ;
- if (!file_exists($dir_path)){
- @mkdir($dir_path, $mode) or die (" Failed to create folder" );
- @chmod($dir_path, $mode);
- } else {
- $dir_path .= "/" ;
- continue ;
- }
- $dir_path .= "/" ;
- }
- return $dir_path ;
- } else {
- @mkdir($dir, $mode) or die( "Creation failed, please check permissions" );
- @chmod($dir, $mode);
- return $dir ;
- }
- } //end makedir
- makedir($path);
-
- /**
- *Get the image on the server based on the url
- *$url The 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
3. Call example
|