Home Backend Development PHP Tutorial php > 依据远程图片URL 生成缩略图

php > 依据远程图片URL 生成缩略图

Jun 13, 2016 pm 12:32 PM
height image img return width

php > 根据远程图片URL 生成缩略图

<?php /**
*
*函数:调整图片尺寸或生成缩略图
*修改:2013-2-15
*返回:True/False
*参数:
*   $Image   需要调整的图片(含路径)
*   $Dw=450   调整时最大宽度;缩略图时的绝对宽度
*   $Dh=450   调整时最大高度;缩略图时的绝对高度
*   $Type=1   1,调整尺寸; 2,生成缩略图
*/

$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;
	}
	// 如果需要生成缩略图,则将原图拷贝一下重新给$Image赋值(生成缩略图操作)
	// 当Type==1的时候,将不拷贝原图像文件,而是在原来的图像文件上重新生成缩小后的图像(调整尺寸操作)
	IF($Type!=1){
		copy($Image,str_replace(".","_x.",$Image));
		$Image=str_replace(".","_x.",$Image);
	}
	// 取得文件的类型,根据不同的类型建立不同的对象
	$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(Empty($Img)){
		// 如果是生成缩略图的时候出错,则需要删掉已经复制的文件
		IF($Type!=1){
			unlink($Image);
		}
		return false;
	}
	// 如果是执行调整尺寸操作则
	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);// 新建一个真彩色画布
		ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);// 重采样拷贝部分图像并调整大小
		ImageJpeg($nImg,$Image);// 以JPEG格式将图像输出到浏览器或文件
		return true;
	} Else {// 如果是执行生成缩略图操作则
		$w=ImagesX($Img);
		$h=ImagesY($Img);
		$width = $w;
		$height = $h;
		$nImg =ImageCreateTrueColor($Dw,$Dh);
		IF($h/$w>$Dh/$Dw){// 高比较大
			$width=$Dw;
			$height=$h*$Dw/$w;
			$IntNH=$height-$Dh;
			ImageCopyReSampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
		} Else {// 宽比较大
			$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;
	}
};



/**
 *根据url获取服务器上的图片
 *$url服务器上图片路径 $filename文件名
*/
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 //网络图片路径
$imgPath = 'http://news.jingway.cn/images/1382088444437.jpg';//远程URL 地址
$tempPath = 'aa/bbs.jpg';//保存图片路径

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

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

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


Copy after login
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1656
14
PHP Tutorial
1257
29
C# Tutorial
1229
24
How to open img file How to open img file Sep 18, 2023 am 09:40 AM

Methods to open img files include using virtual optical drive software, using compression software, and using special tools. Detailed introduction: 1. Use virtual optical drive software to open, download and install a virtual optical drive software, right-click the img file, select "Open with" or "Associated Program", select the installed virtual optical drive software in the pop-up dialog box, virtual The optical drive software will automatically load the img file and use it as a disc image in the virtual optical drive. Double-click the disc icon in the virtual optical drive to open the img file and access its contents, etc.

How to open img file How to open img file Jul 06, 2023 pm 04:17 PM

How to open the img file: 1. Confirm the img file path; 2. Use the img file opener; 3. Select the opening method; 4. View the picture; 5. Save the picture. The img file is a commonly used image file format, usually used to store picture data.

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

What is the format of img? What is the format of img? Mar 17, 2023 am 10:33 AM

img is a file compression format, mainly used to create image files of floppy disks. It can be used to compress the contents of an entire floppy disk or an entire CD; files with the extension ".IMG" are created using this file format. ; The img file includes 3 basic nodes, namely "Ehfa_HeaderTag", "Ehfa_File" and "Ehfa_Entry".

What is the execution order of return and finally statements in Java? What is the execution order of return and finally statements in Java? Apr 25, 2023 pm 07:55 PM

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

How to use Bing Image Creator for free How to use Bing Image Creator for free Feb 27, 2024 am 11:04 AM

This article will introduce seven ways to get high-quality output using the free BingImageCreator. BingImageCreator (now known as ImageCreator for Microsoft Designer) is one of the great online artificial intelligence art generators. It generates highly realistic visual effects based on user prompts. The more specific, clear, and creative your prompts are, the better the results will be. BingImageCreator has made significant progress in creating high-quality images. It now uses Dall-E3 training mode, showing a higher level of detail and realism. However, its ability to consistently produce HD results depends on several factors, including fast

How to delete images from Xiaomi phones How to delete images from Xiaomi phones Mar 02, 2024 pm 05:34 PM

How to delete images on Xiaomi mobile phones? You can delete images on Xiaomi mobile phones, but most users don’t know how to delete images. Next is the tutorial on how to delete images on Xiaomi mobile phones brought by the editor. Interested users can come and join us. Let's see! How to delete images on Xiaomi mobile phone 1. First open the [Album] function in Xiaomi mobile phone; 2. Then check the unnecessary pictures and click the [Delete] button in the lower right corner; 3. Then click [Album] at the top to enter the special area , select [Recycle Bin]; 4. Then directly click [Empty Recycle Bin] as shown in the figure below; 5. Finally, directly click [Permanent Delete] to complete.

What does the width of html mean? What does the width of html mean? Jun 03, 2021 pm 02:15 PM

In HTML5, width means width. The width attribute defines the width of the element's content area. You can add inner margins, borders, and outer margins outside the content area. You only need to set "element {width: value}" to the element.

See all articles