PHP imagick-压缩白色方形

问题描述

这是我的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
);
    
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>

解决方法

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

setCompressionQuality
,

由于Imagick遇到很多麻烦,也许您可​​以改用GD?

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

$img = imagecreatefromjpeg(realpath(PHOTO_PATH.$id.'.jpg'));

header("Content-Type: image/jpg");
imagejpeg($img,null,$compression);