创建缩略图 PHP 问题

发布于 2024-09-04 01:33:09 字数 1733 浏览 2 评论 0原文

我有这个源代码,是从 net tutsplus 获得的。我已经配置了它并使其在一个 PHP 文件中工作。它确实通过传输原始图像来工作,但它不会生成到缩略图文件夹。

<?php

$final_width_of_image = 100;
$path_to_image_directory = "../../img/events/" . urldecode($_GET['name']) . "/";
$path_to_thumbs_directory = "../../img/events/" . urldecode($_GET['name']) . "/thumbnails/";

function createThumbnail($filename)
{
    if(preg_match('/[.](jpg)$/', $filename)) 
    {
        $im = imagecreatefromjpeg($path_to_image_directory . $filename);
    } 
    elseif(preg_match('/[.](gif)$/', $filename)) 
    {
        $im = imagecreatefromgif($path_to_image_directory . $filename);
    } 
    elseif(preg_match('/[.](png)$/', $filename)) 
    {
        $im = imagecreatefrompng($path_to_image_directory . $filename);
    }

    $ox = imagesx($im);
    $oy = imagesy($im);

    $nx = $final_width_of_image;
    $ny = floor($oy * ($final_width_of_image / $ox));

    $nm = imagecreatetruecolor($nx, $ny);

    imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);

    imagejpeg($nm, $path_to_thumbs_directory . $filename);
    $tn = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />';
    echo $tn;
}

if(isset($_FILES['fupload'])) {

    if(preg_match('/[.](jpg)|(gif)|(png)$/', $_FILES['fupload']['name'])) {

        $filename = $_FILES['fupload']['name'];
        $source = $_FILES['fupload']['tmp_name'];
        $target = $path_to_image_directory . $filename;

        move_uploaded_file($source, $target);

        createThumbnail($filename);
    }
}

?>

基本上它应该生成上传图像的缩略图并将原始图像存储到不同的文件夹中。

路径是正确的,它通过获取 URL 中的文件夹名称来工作,它确实有效,但对缩略图文件夹没有任何作用。

在你问这个相关问题之前,是的,缩略图生成确实可以通过 PHP GD 在我的服务器上工作,我已经单独测试过它。所以这不是问题。 :)

我该如何让它发挥作用? :(

I have this source code where I got it from net tutsplus. I have configured it and made it work in one PHP file. It does work by transferring the original image, but it does not generate to the thumbnails folder.

<?php

$final_width_of_image = 100;
$path_to_image_directory = "../../img/events/" . urldecode($_GET['name']) . "/";
$path_to_thumbs_directory = "../../img/events/" . urldecode($_GET['name']) . "/thumbnails/";

function createThumbnail($filename)
{
    if(preg_match('/[.](jpg)$/', $filename)) 
    {
        $im = imagecreatefromjpeg($path_to_image_directory . $filename);
    } 
    elseif(preg_match('/[.](gif)$/', $filename)) 
    {
        $im = imagecreatefromgif($path_to_image_directory . $filename);
    } 
    elseif(preg_match('/[.](png)$/', $filename)) 
    {
        $im = imagecreatefrompng($path_to_image_directory . $filename);
    }

    $ox = imagesx($im);
    $oy = imagesy($im);

    $nx = $final_width_of_image;
    $ny = floor($oy * ($final_width_of_image / $ox));

    $nm = imagecreatetruecolor($nx, $ny);

    imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);

    imagejpeg($nm, $path_to_thumbs_directory . $filename);
    $tn = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />';
    echo $tn;
}

if(isset($_FILES['fupload'])) {

    if(preg_match('/[.](jpg)|(gif)|(png)$/', $_FILES['fupload']['name'])) {

        $filename = $_FILES['fupload']['name'];
        $source = $_FILES['fupload']['tmp_name'];
        $target = $path_to_image_directory . $filename;

        move_uploaded_file($source, $target);

        createThumbnail($filename);
    }
}

?>

Basically it is supposed to generate a thumbnail of the uploaded image and store the original image into a different folder.

The paths are correct, it works by getting the folder name in the URL, it does work, but nothing works for the thumbnails folder.

BEFORE you ask this related question, yes, thumbnails generation does work on my server by the PHP GD, I have tested it separately. So this is not the problem. :)

How do I get this to work? :(

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

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

发布评论

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

评论(1

帅气尐潴 2024-09-11 01:33:09

首先,使用 imagecopyresampled(),因为它将生成更好的缩略图。

其次,您不应该对文件系统目录和 url 目录使用相同的变量。您应该有 $filesystem_path_to_thumbs$url_path_to_thumbs。所以你可以对它们进行不同的设置。

第三,您可能需要检查宽度和高度的尺寸。如果有人上传很高的图片会怎样?您的缩略图将位于目标框之外(但这可能不是一个大问题)。

第四,在生成缩略图之前,您应该检查缩略图文件是否存在于thumbs目录中(出于性能原因)...

最后,它实际上失败的原因是$final_width_of_image$path_to_image_directory$path_to_thumbs_directory 未在函数内定义。要么:

  1. 在函数开始时将它们设为全局,以便您可以在函数内部访问它们 global $final_width_of_image, $path_to_image_directory, $path_to_thumbs_directory;

  2. 将它们作为函数的参数:function createTumbnail($image, $width, $path_to_images, $path_to_thumbs) {

  3. 在函数内部对它们进行硬编码(将它们的声明从函数外部移动到函数内部)。

    在函数内部对它们进行

就我个人而言,我会做#2,但这取决于你的要求和需求......

Well, first off, use imagecopyresampled(), as it will generate a better thumbnail.

Secondly, you shouldn't use the same variable for filesystem directory and for url directory. You should have $filesystem_path_to_thumbs and $url_path_to_thumbs. So you can set them differently.

Third, you may want to do a size check for both width and height. What happens if someone uploads a tall image? Your thumbnail will be outside the target box (but this may not be a big issue).

Fourth, you should prob do a check to see if the thumbnail file exists in the thumbs directory before generating the thumbnail (for performance reasons)...

Finally, the reason it's actually failing, is $final_width_of_image, $path_to_image_directory and $path_to_thumbs_directory are not defined within the function. Either:

  1. Make them global at the start of the function, so you can access them inside of the function global $final_width_of_image, $path_to_image_directory, $path_to_thumbs_directory;

  2. Make them arguments to the function: function createTumbnail($image, $width, $path_to_images, $path_to_thumbs) {

  3. Hard code them inside of the function (Move their declaration from outside the function to inside the function).

Personally, I'd do #2, but it's up to what your requirements and needs are...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文