2010-07-18 8 views
0

Im essayant de créer une fonction simple redimensionnement d'image, pouvez-vous me dire où je vais mal:Image Resize PHP

//resize image 
    $imageSrc = imagecreatefromjpeg('http://www.katsmurals.com/cms/'.$target_path); 
    $width = imagesx($imageSrc); 
    $height = imagesy($imageSrc); 
    echo $width; 
    echo $height; 
    //new dimensions 
    $i = $width; 
    $j = $height; 
    while($i > 700){ 
    $i = $i/1.5; 
    $j = $j/1.5; 
    } 

    $image_p = imagecreatetruecolor($i, $j); 
    $image = imagecreatefromjpeg('http://www.katsmurals.com/cms/'.$target_path); 
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $i, $j, $width, $height); 
    $newImage = $target_path.'_scaled.jpg'; 
    imagejpeg($image_p, $newImage, 80); 

TARGET_PATH de $ est l'image nouvellement téléchargé, je veux redimensionner si la largeur est plus de 700.

Une fois cela fait, un DB est mis à jour avec les informations, au moment où l'image est bien téléchargée, mais la taille semble être exactement la même, donc le code de redimensionnement ne fonctionne pas?

+0

Possible copie de [redimensionner l'image en PHP] (http://stackoverflow.com/questions/14649645/resize-image-in-php) –

Répondre

0

http://php.net/manual/en/function.imagecopyresampled.php

Le premier argument de imagecopyresampled est ressource (image), non chaîne contenant le nom du fichier.

+0

J'ai changé le code, toujours rien, pouvez-vous voir où je vais mal ? – user195257

+1

@user s'il vous plaît faire un peu de débogage. Quelles sont les valeurs 'width' et' height'? Quel genre d'image redimensionnez-vous? Quel format? –

+0

fichier .jpg, lors de l'écho out largeur et hauteur ce sont les résultats: largeur: 1235 hauteur: 1551 la boucle while fonctionne avec succès pour donner de nouvelles valeurs, la création de la nouvelle image redimensionnée est le problème. – user195257

1

Un très utile et le code étonnant:

<?php 
    class SimpleImage { 

    var $image; 
    var $image_type; 

    function load($filename) { 

    $image_info = getimagesize($filename); 
    $this->image_type = $image_info[2]; 
    if($this->image_type == IMAGETYPE_JPEG) { 

    $this->image = imagecreatefromjpeg($filename); 
    } elseif($this->image_type == IMAGETYPE_GIF) { 

    $this->image = imagecreatefromgif($filename); 
    } elseif($this->image_type == IMAGETYPE_PNG) { 

    $this->image = imagecreatefrompng($filename); 
    } 
    } 
    function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75,  $permissions=null) { 

    if($image_type == IMAGETYPE_JPEG) { 
    imagejpeg($this->image,$filename,$compression); 
    } elseif($image_type == IMAGETYPE_GIF) { 

    imagegif($this->image,$filename); 
    } elseif($image_type == IMAGETYPE_PNG) { 

    imagepng($this->image,$filename); 
    } 
    if($permissions != null) { 

    chmod($filename,$permissions); 
    } 
    } 
    function output($image_type=IMAGETYPE_JPEG) { 

    if($image_type == IMAGETYPE_JPEG) { 
    imagejpeg($this->image); 
    } elseif($image_type == IMAGETYPE_GIF) { 

    imagegif($this->image); 
    } elseif($image_type == IMAGETYPE_PNG) { 

    imagepng($this->image); 
    } 
    } 
    function getWidth() { 

    return imagesx($this->image); 
    } 
    function getHeight() { 

    return imagesy($this->image); 
    } 
    function resizeToHeight($height) { 

    $ratio = $height/$this->getHeight(); 
    $width = $this->getWidth() * $ratio; 
    $this->resize($width,$height); 
    } 

    function resizeToWidth($width) { 
    $ratio = $width/$this->getWidth(); 
    $height = $this->getheight() * $ratio; 
    $this->resize($width,$height); 
    } 

    function scale($scale) { 
    $width = $this->getWidth() * $scale/100; 
    $height = $this->getheight() * $scale/100; 
    $this->resize($width,$height); 
    } 

    function resize($width,$height) { 
    $new_image = imagecreatetruecolor($width, $height); 
    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 
    $this->image = $new_image; 
    }  

} 
?> 

Lien: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php je l'ai trouvé très helpful.hope bon pour vous. Codage heureux !!