PHP获取网络图片并显示到浏览器

本文介绍了一个简单的PHP脚本,该脚本可以从用户提供的URL抓取JPEG图片,将其缩放到一半大小,并直接在浏览器中显示。此过程涉及使用HTML表单接收输入,通过PHP处理图像并进行尺寸调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

getimage.html

<!DOCTYPE html>
<html>
<head>
<title>Get Image From Web</title>
</head>
<body>
<form method = "post" action = "getimage.php">
	<label for="URL">URL:</label>
	<input type="text" name="url" />
	<input type="submit" name="submit" value="Get IMG" />
</form>
</body>
</html>

getimage.php

<?php
// set up variable
$url = $_REQUEST['url'];
// echo "<p> 8 ".$url."<br /></p>";
$ext = substr($url, strrpos($url, '.')+1);
// echo '<p> 9 '.$ext.'<br /></p>';
$filename = substr($url, strrpos($url, '/')+1, -(strlen($ext) + 1));
// echo '<p> 10 '.$filename.'<br /></p>';

if ($ext == 'jpg') {
	$im = imagecreatefromjpeg($url);
	if ($im) {
//		echo '<p>created image handle<br /></p>';
		$width = imagesx($im);
		$height = imagesy($im);
		$x = $width/2;
		$y = $height/2;


		$dst = imagecreatetruecolor($x, $y);
		imagecopyresampled($dst, $im, 0, 0, 0, 0, $x, $y, $width, $height);
		header("Content-Type:image/jpeg");
//		imagejpeg($dst, 'imgdst.jpg');
		imagejpeg($dst);
		imagedestroy($dst);
		imagedestroy($im);
//		echo '<img src="imgdst.jpg" width="300">';
	}
}


?>

以上处理是将收到的url中的网页转换为图片,缩放后再显示到浏览器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值