使用 PHP 在服务器端调整 Jpeg 的大小

发布于 2024-12-16 16:11:22 字数 707 浏览 4 评论 0原文

我想在服务器端调整图像大小以动态制作缩略图。 我正在使用这段代码:

<?php
Header("Content-type: image/jpeg"); 
$img_src = $_GET['photo']; 

$size = getimagesize($img_src);

$src_w = $size[0];
$src_h = $size[1]; 

$dst_w = 80;
$dst_h = 80;

$test_h = round(($dst_w / $src_w) * $src_h); 
$test_w = round(($dst_h / $src_h) * $src_w); 


if($src_w > $src_h) {
    $x = $test_w;
    $y = $dst_h;
} elseif($src_h > $src_w) {
    $x = $dst_w;
    $y = $test_h;
}

$img_new = imagecreatefromjpeg($img_src); 
$img_mini = imagecreatetruecolor($x, $y); 
imagecopyresampled($img_mini,$img_new,0,0,0,0,$x,$y,$src_w,$src_h); 
imagejpeg($img_mini); 
?>

但它不起作用,我找不到原因。没有错误,只是什么也没出现。 有人可以帮助我吗?

谢谢;

I would like to resize images on server side to make thumbnail dynamicaly.
I'm using this code :

<?php
Header("Content-type: image/jpeg"); 
$img_src = $_GET['photo']; 

$size = getimagesize($img_src);

$src_w = $size[0];
$src_h = $size[1]; 

$dst_w = 80;
$dst_h = 80;

$test_h = round(($dst_w / $src_w) * $src_h); 
$test_w = round(($dst_h / $src_h) * $src_w); 


if($src_w > $src_h) {
    $x = $test_w;
    $y = $dst_h;
} elseif($src_h > $src_w) {
    $x = $dst_w;
    $y = $test_h;
}

$img_new = imagecreatefromjpeg($img_src); 
$img_mini = imagecreatetruecolor($x, $y); 
imagecopyresampled($img_mini,$img_new,0,0,0,0,$x,$y,$src_w,$src_h); 
imagejpeg($img_mini); 
?>

But it didn't work, and I can't find why. There is no error, juste nothing appeared.
Can anybody help me ?

Thanks;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

腹黑女流氓 2024-12-23 16:11:22

如果您收到空白页,则说明您的脚本超时或超出了 php 内存限制。在启动任何 gd 函数之前,使用 ini_set 函数设置 memory_limitmax_execution_time

图像处理需要时间和内存。所以这些配置很重要。

If you are getting a blank page, then either your script timed out or php memory limit was exceeded. Use ini_set function to set memory_limit and max_execution_time before starting any gd function.

Image manipulations take time as well as memory. So these configurations are important.

暖树树初阳… 2024-12-23 16:11:22

您可能需要将您获取的 url 转换为实际的文件路径,以便可以打开该文件。我希望您使用的函数需要文件路径,而不是 url。如果没有,他们应该这样做,否则您将执行 2 个额外请求,只是为了避免下载一张图像并通过客户端上的 CSS 调整大小。

无论如何,如果您要调整缩略图大小,您确实应该将缩略图缓存在 thumbs (或其他)子目录中。然后,在每个请求中,检查缩略图是否存在,如果存在则直接提供,然后再创建缩略图。或者,如果文件不是动态上传的,请预先创建缩略图并直接从 thumbs 目录引用它们。

You probably need to translate the url you are getting into an actual file path so that you can open the file. I expect that the functions you are using expect a file path, not a url. If not, they should otherwise you're doing 2 extra requests just to avoid downloading one image and resizing via CSS on the client.

In any event, if you're going to resize to a thumbnail you really should be caching the thumbnail in a thumbs (or other) subdirectory. Then on each request, check if the thumbnail is exists, serving it up directly if so, before creating the thumbnail. Or, if the files aren't dynamically uploaded, pre-create the thumbnails and just reference them from the thumbs directory directly.

千紇 2024-12-23 16:11:22

检查这一行:

$size = getimagesize($img_src);

您在哪里定义了 $img_src?我看不到!

编辑
使用此代码将文件写入磁盘,然后重定向到写入的文件:

<?php

$chemin = $_GET['photo']; 

$size = getimagesize($chemin);


$src_w = $size[0];
$src_h = $size[1]; 

$dst_w = 80;
$dst_h = 80;

$test_h = round(($dst_w / $src_w) * $src_h); 
$test_w = round(($dst_h / $src_h) * $src_w); 


if($src_w > $src_h) {
    $x = $test_w;
    $y = $dst_h;
} elseif($src_h > $src_w) {
    $x = $dst_w;
    $y = $test_h;
}

$img_new = imagecreatefromjpeg($chemin); 
$img_mini = imagecreatetruecolor($x, $y); 

imagecopyresampled($img_mini, $img_new, 0, 0, 0, 0, $x ,$y, $src_w, $src_h); 

$name=rand() * rand() . '.jpg';

imagejpeg($img_mini, $name); 
header("Location: $name");

?>

Check this line:

$size = getimagesize($img_src);

Where have you defined $img_src? I can't see it!

EDIT
Use this code to write the file to disk and then redirect to the written file:

<?php

$chemin = $_GET['photo']; 

$size = getimagesize($chemin);


$src_w = $size[0];
$src_h = $size[1]; 

$dst_w = 80;
$dst_h = 80;

$test_h = round(($dst_w / $src_w) * $src_h); 
$test_w = round(($dst_h / $src_h) * $src_w); 


if($src_w > $src_h) {
    $x = $test_w;
    $y = $dst_h;
} elseif($src_h > $src_w) {
    $x = $dst_w;
    $y = $test_h;
}

$img_new = imagecreatefromjpeg($chemin); 
$img_mini = imagecreatetruecolor($x, $y); 

imagecopyresampled($img_mini, $img_new, 0, 0, 0, 0, $x ,$y, $src_w, $src_h); 

$name=rand() * rand() . '.jpg';

imagejpeg($img_mini, $name); 
header("Location: $name");

?>
宫墨修音 2024-12-23 16:11:22

如果您没有收到错误,我将使用您知道的测试图像,并逐行处理此问题,确认您得到了预期的结果,直到找到导致问题的行。检查这些函数的预期返回结果,并在代码卡住或未获得所需结果的位置输出条件条件。

例子:

if (isset($img_src)) {
    echo "No image parameter set in the URL";
    return;
}

If you are not getting an error, I would work with a test image that you know, and work through this line by line, confirming that you are getitng the results you are expecting, untily you find the line(s) that are causing issues. Check the expected return results for those functions and make conditionals that output where this code is getting stuck or where you are not getting the result you want.

Example:

if (isset($img_src)) {
    echo "No image parameter set in the URL";
    return;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文