无法在php中显示调整大小的图像的URL

时间:2015-01-01 21:16:10

标签: php html image

我可以使用php中的库来帮助调整大小。但是我无法显示调整大小的图像网页的网址。我正在调整图像的大小,然后我想显示调整大小的图像的网址。 我能够显示图像,但不能显示调整大小的图像的网址。请告诉我如何通过查看下面的代码显示修改后的图像的网址

<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-Type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filename);
//echo $width;
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source,0,0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
echo imagejpeg($thumb);



?>

3 个答案:

答案 0 :(得分:1)

您尚未将调整后的图像保存在任何位置,因此除非您将其保存在某处,否则只能从此脚本的网址获取已调整大小的图像。
要保存图像,只需指定imagejpeg

的名称即可
// save
imagejpeg($thumb,'test-sm.jpg');
//Output
readfile('test-sm.jpg');

图像的网址将是当前目录+您保存为的名称。

答案 1 :(得分:0)

假设您发布的脚本是imageResized.php 然后另一页会有:

<img src="imageResized.php" /> 

答案 2 :(得分:0)

使用此脚本

function curPageURL() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

http://webcheatsheet.com/php/get_current_page_url.php获取脚本名称。您请求服务器的URL将成为图像的URL。

但是,您将无法像在此调用中那样在浏览器中显示此内容,因为此脚本只是获取图像。您可以使用此代码获取要写入日志文件的URL。