PHP > according to the remote URL picture thumbnail generation

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
<?php
/**
*
*Function: to adjust the image size or thumbnail generation
*Modified: 2013-2-15
*Returns: True/False
*Parameters:
*   $Image needs to adjust the picture (including path)
*   $The maximum width of Dw=450 adjustment; absolute width of thumbnails
*   $The maximum height of Dh=450 adjustment; the absolute height of the thumbnail
*   $Type=1   1,Adjust the size; 2, generating thumbnail
*/

$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 thumbnails, the original copy to the assignment to $Image (thumbnail generation operation)
	// When Type==1, will not copy the original image, but the image file the original re generated image after narrowing (resize operation)
	IF($Type!=1){
		copy($Image,str_replace(".","_x.",$Image));
		$Image=str_replace(".","_x.",$Image);
	}
	// Get file types, according to the different types of different objects
	$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 successful, the non image files
	IF(Empty($Img)){
		// If it is time to generate thumbnails error, you need to delete the file has been copied
		IF($Type!=1){
			unlink($Image);
		}
		return false;
	}
	// If it is executed in adjusting the size of 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);// A new color canvas
		ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);// Resampling copy part of the image and adjust the size
		ImageJpeg($nImg,$Image);// The image output to the browser or file in JPEG format
		return true;
	} Else {// If it is executed in thumbnail generation operation
		$w=ImagesX($Img);
		$h=ImagesY($Img);
		$width = $w;
		$height = $h;
		$nImg =ImageCreateTrueColor($Dw,$Dh);
		IF($h/$w>$Dh/$Dw){// High high
			$width=$Dw;
			$height=$h*$Dw/$w;
			$IntNH=$height-$Dh;
			ImageCopyReSampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
		} Else {// Aspect 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;
	}
};



/**
 *According to the URL access server.
 *$Picture path to the $filename file on the URL server 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;
}
?>




<?php
//Network image path
$imgPath = 'http://news.jingway.cn/images/1382088444437.jpg';//The remote URL address
$tempPath = 'aa/bbs.jpg';//Save the image path

if(is_file($tempPath)){
	unlink($tempPath);
}

$bigImg=GrabImage($imgPath, $tempPath);

compressImg($bigImg,70,70,1);
?>


</body>
</html>

 

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Gladys at December 07, 2013 - 9:08 AM