【问题标题】:PHP imagick - white square on compressionPHP imagick - 压缩上的白色方块
【发布时间】:2020-08-13 09:58:08
【问题描述】:

这是我的 PHP 代码(使用 Imagick),用于在显示图像之前对其进行压缩。

但是,每次我运行它,结果都是一样的:一个带有白色边框的黑色方块,都没有任何 php 错误。

你能解释一下问题出在哪里吗?

谢谢。

$id = $_GET['id'];
$compression = $_GET['compr'];

$backgroundImagick = new Imagick(realpath(PHOTO_PATH.$id.'.jpg'));
$imagick = new Imagick();
$imagick->setCompression(Imagick::COMPRESSION_JPEG);
$imagick->setCompressionQuality($compression);
$imagick->newPseudoImage(
    $backgroundImagick->getImageWidth(),
    $backgroundImagick->getImageHeight(),
    'canvas:white'
);

$imagick->compositeImage(
    $backgroundImagick,
    Imagick::COMPOSITE_ATOP,
    0,
    0
);
    
header("Content-Type: image/jpg");
echo $imagick->getImageBlob(); 

还有,结果截图:image

编辑:我的 htaccess 代码

## contrôle du cache navigateur - Expire headers
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 7200 seconds"
    ExpiresByType image/jpg             "access plus 1 week"
    ExpiresByType image/jpeg            "access plus 1 week"
    ExpiresByType image/png             "access plus 1 week"
    ExpiresByType image/gif             "access plus 1 week"
    ExpiresByType image/svg+xml         "access plus 1 week"
    AddType image/x-icon .ico
    ExpiresByType image/ico             "access plus 1 week"
    ExpiresByType image/icon            "access plus 1 week"
    ExpiresByType image/x-icon          "access plus 1 week"
    ExpiresByType text/css              "access plus 1 week"
    ExpiresByType text/javascript       "access plus 1 week"
    ExpiresByType text/html             "access plus 7200 seconds"
    ExpiresByType application/xhtml+xml     "access plus 7200 seconds"
    ExpiresByType application/javascript    "access plus 1 week"
    ExpiresByType application/x-javascript  "access plus 1 week"
    ExpiresByType application/x-shockwave-flash "access plus 1 week"
</IfModule>

ErrorDocument 404 /index.php?page=404
ErrorDocument 403 /index.php?page=403

Options -Indexes
RewriteEngine on
Options All -Indexes

# AJOUT SLASH FIN URL 
RewriteCond %{REQUEST_URI} (/[^.]+)[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=307,L] 

# STANDARD
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?page=$1 [NC,L,QSA]

最后是 index.php 文件:

<?php
require('include.php');

if(isset($_GET['page'])){
    $Page = strtolower($_GET['page']);
    $Page = str_replace('/', '', $Page);
}
else{
    $titre = SITENAME. ' | accueil';
    $desc = 'Accueil de '.SITENAME;
    $Page = 'index';
}

if(!empty($_POST) && isset($_POST['form'])){
    $form = $_POST['form'];
    if(is_file('php/doForm/'.$form.'.php')){
        require('php/doForm/'.$form.'.php');
    }
}

switch($Page){
    case 'login' :  $titre = SITENAME.' | Se connecter'; 
                    $desc = 'Se connecter à son compte | '.SITENAME;
    break;
    case 'register' :   $titre = SITENAME.' | Création de compte';
                        $desc = 'Se créer un compte sur '.SITENAME;
    break;
    case 'collections' :    $titre = SITENAME. ' | Collections';
                            $desc = 'Collections de photos | '.SITENAME;
    break;
    case 'administration'   :   $titre = SITENAME. ' | Administration';
                                $desc = 'Admin';
    break;
    case 'profil'   :   $titre = SITENAME. ' | Votre profil';
                        $desc = "Consulter et modifier vos informations de compte sur ".SITENAME;
    break;
    default :   $titre = SITENAME;
                $desc = SITENAME;
    break;
}


?>

<html>

    <style>
        :root{
            --background: <?= $Vars->GetVar('css_background') ?>;
            --inv-background: <?= $Vars->GetVar('css_inv-background') ?>;
            --color: <?= $Vars->GetVar('css_color') ?>; 
            --gris: <?= $Vars->GetVar('css_gris') ?>;
            --color-fonce: <?= $Vars->GetVar('css_color-fonce') ?>;
            --color-fonce1: <?= $Vars->GetVar('css_color-fonce1') ?>;
            --color-fonce2: <?= $Vars->GetVar('css_color-fonce2') ?>;
            --section-2: <?= $Vars->GetVar('css_section-2') ?>;
            --cadre-photos: <?= $Vars->GetVar('css_cadre-photos') ?>;
            --titres-section2: <?= $Vars->GetVar('css_titres-section2') ?>;
            --utm: 'UTMNeutra';
            --def-font: Verdana, Geneva, Tahoma, sans-serif;
        }
    </style>
    <head>
        <title> <?= $titre ?> </title>
        <meta name='description' content="<?=$desc?>" />
        <link rel="stylesheet" href="<?= URL.'css/style.css' ?>" />     
        <meta name='keywords' content="<?= KEYWORDS ?>" />
    </head>
    <body>
        <div class='header'>
            <?php include "pages/header.php"; ?>
        </div>

        <div class='content'>
            <?php 
                switch($Page) {
                    case '404' :    include('pages/404.html');

                    break;

                    case '403' :    include('pages/404.html');
                                    header("HTTP/1.0 403 Missing permissions", true, 403);
                    break;

                    case 'admini' : include "administration/index.php";
                    break;

                    default :   if(@is_file("pages/{$Page}.php"))
                                    include "pages/{$Page}.php";        
                                else
                                    $Page = 404;
                                    include "pages/404.html";
                                    include('pages/404.html');
                    break;
                }
            ?>
            
        </div>  

        <div class='footer'>
            <?php include "pages/footer.php"; ?>
        </div>
    </body>
</html>

【问题讨论】:

    标签: php web imagick


    【解决方案1】:

    我的代码也出现错误,但发现您可以直接在 $backgroundImagick 上设置压缩,而无需使用 setImageCompressionQuality 而不是 setCompressionQuality 创建新的合成图像:

    $id = $_GET['id'];
    $compression = $_GET['compr'];
    
    $backgroundImagick = new Imagick(realpath(PHOTO_PATH.$id.'.jpg'));
    $backgroundImagick->setImageCompressionQuality($compression);
    
    header("Content-Type: image/jpg");
    echo $backgroundImagick->getImageBlob(); 
    

    【讨论】:

    • 感谢您的回答!不幸的是,我仍然有相同的结果...(我的基本图像是 .jpg,这不是问题吗?)
    • 我的测试是 jpg,所以我不这么认为。它只是您遇到问题的一个图像还是全部?我的测试是在 PHP 7.3 上使用 Imagick 3.4.4 (ImageMagick 6.9.7-4 Q16 x86_64 20170114) 完成的。你有什么版本,所以我可以尝试复制?您可以分享失败的图像吗?
    • 嗨,我刚刚用另一张图片重新测试,但仍然得到相同的结果。 (有问题的图像:imgur)对于版本,我在 Debian 10 上,我有 php 7.3.19-1~deb10u1 和 Imagick 3.4.3
    • 刚刚用你的图片测试过,我的代码对我来说很好,你的版本也没有太大的不同。我现在想知道您是否可以在浏览器中获取缓存图像?您是否尝试过使用开发人员工具禁用缓存?或者,它可能是从错误的文件夹加载的,您是否尝试过硬编码图像路径?
    • 我只是把有问题的照片的路径,清空了缓存,但还是同样的问题,并且没有php错误。
    【解决方案2】:

    由于您在使用 Imagick 时遇到了很多麻烦,也许您可​​以使用 GD 来代替?

    $id = $_GET['id'];
    $compression = $_GET['compr'];
    
    $img = imagecreatefromjpeg(realpath(PHOTO_PATH.$id.'.jpg'));
    
    header("Content-Type: image/jpg");
    imagejpeg($img,null,$compression);
    

    【讨论】:

    • 好的,所以不是来自代码,而是来自 htaccess(不是照片的路径)。我不知道为什么,但是当我访问 URL“site.com/showimage/?id=xxxxx&compr=75”时,我得到白色方块,但当我访问页面时没有(“site.com/pages/showimage .php?id=xxxxx&compr=75")。知道我为什么会遇到这个问题吗?
    • 啊,这很有趣。如果是的话,您是否使用 PHP 框架?一般来说,对于这样的问题,我会使用 xdebug 逐步检查代码以查看发生了什么。我想知道是否在代码中的某处设置了额外的标头,我会在浏览器的开发人员工具中检查响应。如果没有所有代码,我将很难进一步提供帮助,但至少您向前迈出了一步。
    • 我不使用框架,你想要什么代码?重定向或压缩代码? (顺便说一下,没有发送额外的标头)。
    • 好的,在这种情况下,您能否将 htaccess 代码添加到问题中,我会尝试在我的版本中使用它吗?此外,如果您已经提供的之前或之后有任何相关代码
    • 谢谢,从我可以看到它重写为index.php,index.php中的showimage.php是如何包含的,是简单的require还是include?可以在您的问题中添加 index.php 代码吗?此外,可能值得在开发时更改 htaccess 中的 mod_expires,将 image/jpg 设置为非常小的值,这样您就可以消除缓存问题。之后您需要再次清除浏览器缓存。还将 ExpiresDefault 设置为非常小的值。
    猜你喜欢
    • 2011-11-19
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 2011-06-25
    • 2014-01-10
    相关资源
    最近更新 更多