Plugin Media -> Pas de redimension

Par Yann Ciczora, il y a 13 ans


Bonjour,

J'utilise la plugin Media, mais j'ai un problème majeur.

lorsque j'utilise la fonction sprintf, il n't a pas de redimension de l'image:

echo $this->Html->image(sprintf($d'Post']'thumbf'],80,200));

Mon route.php

Router::connect('/img/:file_:format.jpg',array('controller'=>'medias', 'action'=>'crop','plugin'=>'media'),
                                array(
                                'file'=>'posts\/([0-9]+)\/^\/]+',
                                'format'=>'80x200'
                                ));
    Router::connect('/img/:file_:format.jpg', array('controller' => 'medias', 'action' => 'crop', 'plugin' => 'media' ),
                                array(
                                'file' => 'uploads\/[0-9]{4}\/[0-9]{2}\/^\/]+',
                                'format' => '80x200|200x150'
                                ));

Mon PostController :

public $actsAs = array(
                'Containable',
                'Media.Media' => array(
                    'path'=>'posts/%mid/%f')
            );

D'où cela peut-il venir?

3 réponses

Ciloe, il y a 13 ans

Pourquoi tu utilises sprintf()?

Il faut utiliser des fonction de redimensionnement d'image pour les redimensionner....

Yann Ciczora, il y a 13 ans

Bah non, j'ai suivi le tuto. Il y a cette fonction pour redimensionner et afficher l'image. C'est prévu par le plugin 'Media'
Sauf que j'ai moi, ça ne marche pas...

Ciloe, il y a 13 ans

Oui justement(c'est ça l'erreur), je comprends pas comment on peut utiliser sprintf pour redimensionner une image... Tiens voilà une fonction de redimensionnement (c'est celle de Grafikart):

public function imageresize($source, $destination, $width = 0, $height = 0, $crop = false, $quality = 80) {
        $quality = $quality ? $quality : 80;
        $data = explode('.', $source);
        $ext = end($data);
        switch ($ext) {
            case 'jpeg':
            case 'jpg':
                $image = imagecreatefromjpeg($source);
                break;
            case 'gif':
                $image = imagecreatefromgif($source);
                break;
            case 'png':
                $image = imagecreatefrompng($source);
                break;
            default:
                $image = false;
                break;
        }
        if ($image) {
            $w = imagesx($image);
            $h = imagesy($image);
            if (($width && $w > $width) || ($height && $h > $height)) {
                $ratio = $w / $h;
                if (($ratio >= 1 || $height == 0) && $width && !$crop) {
                    $new_height = $width / $ratio;
                    $new_width = $width;
                } elseif ($crop && $ratio <= ($width / $height)) {
                    $new_height = $width / $ratio;
                    $new_width = $width;
                } else {
                    $new_width = $height * $ratio;
                    $new_height = $height;
                }
            } else {
                $new_width = $w;
                $new_height = $h;
            }
            $x_mid = $new_width * .5; //horizontal middle
            $y_mid = $new_height * .5; //vertical middle
            // Resample
            error_log('height: '.$new_height.' - width: '.$new_width);
            $new = imagecreatetruecolor(round($new_width), round($new_height));
            imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $w, $h);
            // Crop
            if ($crop) {
                $crop = imagecreatetruecolor($width ? $width : $new_width, $height ? $height : $new_height);
                imagecopyresampled($crop, $new, 0, 0, ($x_mid - ($width * .5)), 0, $width, $height, $width, $height);
                //($y_mid - ($height * .5))
            }
            // Output
            // Enable interlancing [for progressive JPEG]
            imageinterlace($crop ? $crop : $new, true);
            $dext = strtolower(pathinfo($destination, PATHINFO_EXTENSION));
            if ($dext == '') {
                $dext = $ext;
                $destination .= '.' . $ext;
            }
            switch ($dext) {
                case 'jpeg':
                case 'jpg':
                    imagejpeg($crop ? $crop : $new, $destination, $quality);
                    break;
                case 'png':
                    $pngQuality = ($quality - 100) / 11.111111;
                    $pngQuality = round(abs($pngQuality));
                    imagepng($crop ? $crop : $new, $destination, $pngQuality);
                    break;
                case 'gif':
                    imagegif($crop ? $crop : $new, $destination);
                    break;
            }
            @imagedestroy($image);
            @imagedestroy($new);
            @imagedestroy($crop);
            return true;
        }else{
            return false;
        }
    }

Avec cette fonction tu redimensionne ton image. Tu lui donne la source, la destination, les dimensions, le crope et la qualité. Voilà ^^