MathiAs2Pique_
MathiAs2Pique_

Reputation: 153

PHP imagick - white square on compression

Here is my PHP code (with Imagick) that I use to compress an image before displaying it.

However, every time I run it, the result is the same: a black square with white borders, all without any php errors.

Can you please explain to me where the problem comes from?

Thank you.

$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(); 

And, the screenshot of the result :image

EDIT : My htaccess code

## 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]

And finally the index.php file :

<?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>

Upvotes: 2

Views: 257

Answers (2)

Mark
Mark

Reputation: 1039

As you are having a lot of trouble with Imagick perhaps you could use GD instead?

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

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

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

Upvotes: 1

Mark
Mark

Reputation: 1039

I was getting an error too with your code but found you could simply set the compression on the $backgroundImagick directly without creating the new composite image by using setImageCompressionQuality instead of 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(); 

Upvotes: 1

Related Questions