[英]How to use PHP functions from external file in HTML image tag?
我有一个外部PHP文件,可以调整图像大小(image.php)。 我每次使用'image'标签时都需要调用它,这样它就可以填入“src”。 我如何告诉HTML文件使用外部PHP页面?在处理'image'“src”标签时如何使用它?
这是我用来试图显示图像。
我是否需要在“头部”中加入一些内容?
谢谢您的帮助!
<li><a href= "/intPics//1.jpg"> <img src="<?php loadImage('/intPics//1.jpg', 300,300) ?>"<div>1.jpg</div></a></li>
这是PHP文件
<?php
function imageResizer($url, $width, $height) {
header('Content-type: image/jpeg');
list($width_orig, $height_orig) = getimagesize($url);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// This resamples the image
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($url);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output the image
imagejpeg($image_p, null, 100);
}
/ /works with both POST and GET
$method = $_SERVER['REQUEST_METHOD'];
if ($method == 'GET') {
imageResize($_GET['url'], $_GET['w'], $_GET['h']);
} elseif ($method == 'POST') {
imageResize($_POST['url'], $_POST['w'], $_POST['h']);
}
// makes the process simpler
function loadImage($url, $width, $height){
echo 'image.php?url=', urlencode($url) ,
'&w=',$width,
'&h=',$height;
}
?>
我不知道我是否理解你的问题,但我会尽力回答:
您可以直接从图像标记中的src
调用php脚本
<img src="image.php" />
并在您的PHP文件中,您将内容类型设置为图像格式
<?php header('Content-Type:image/jpeg'); ?>
这样php脚本返回一个图像。 您甚至可以将参数传递给php脚本,例如image.php?width=200&height=100
。
我不知道你的php文件中有什么,但是
<?php require_once('path/image.php') ?>
应该导入它,然后你只需使用这些函数并通过php脚本填充src?
编辑:如果您提供更多信息,请提供代码,我可以帮助您就详细信息提出进一步的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.