Tema: PHP Imagick
Ver Mensaje Individual
  #3 (permalink)  
Antiguo 12/10/2011, 07:20
Avatar de DeeR
DeeR
 
Fecha de Ingreso: diciembre-2003
Ubicación: Santiago
Mensajes: 520
Antigüedad: 20 años, 4 meses
Puntos: 17
Respuesta: PHP Imagick

Hola JavierCoreezx

Hay muchas formas de hacer un thumb en PHP, pero lo más común es utilizar GD en vez de ImageMagick.

En general, lo que se hace, es obtener un ratio de proporción, entre las dimensiones actuales y las nuevas, luego se hace una nueva imágen y copiamos la imágen anterior con la función imagecopyresampled.

Hay varios ejemplos en el foro como en la red, de todas formas te dejo mi función (apuntes de gd)


Código PHP:
<?php

// Function by deerme.org
function imagethumb($img,$width_new=100,$savepath false)
{
        if ( !
is_file$img  )  )
        {
                throw new 
Exception('Image ('.$img.') not found.');
                return 
false;
        }
        
// Max Height
        
$height_max=1024;
        
$imginfo getimagesize($img);
        if ( !
$imginfo  )
        {
                throw new 
Exception('The file '.$img.' it is not a valid image.');
                return 
false;
        }
        switch( 
$imginfo  )
        {
                case 
$imginfo[2] == :
                        
$imggd imagecreatefromgif($img);
                break;

                case 
$imginfo[2] == :
                        
$imggd imagecreatefromjpeg($img);
                break;

                case 
$imginfo[2] == :
                        
$imggd imagecreatefrompng($img);
                break;
        }
        
$ratio = ($imginfo[0]/$width_new);
        
$height_new = (int)($imginfo[1] / $ratio);

        if(
$height_new>$height_max)
        {
            
$width_new = (int) ($height_max*$width_new/$height_new);
            
$height_new $height_max;
        }
        
        
$imggdthumb imagecreatetruecolor($width_new,$height_new); 
        
imagecopyresampled($imggdthumb$imggd0000$width_new$height_new$imginfo[0], $imginfo[1]); 
        
        if ( 
$savepath )
        {
            if ( 
is_writabledirname($savepath )) )
            {
                
imagejpeg($imggdthumb,$savepath,100);
                
imagedestroy($imggdthumb);
                return 
$savepath;
            }
            else
            {
                throw new 
Exception('The path ('.$savepath.') is not writable.');
                return 
false;
            }
        }
        else
        {
            
header("Content-type: image/jpeg");
            
imagejpeg($imggdthumb,false,100);
            
imagedestroy($imggdthumb);
            return 
true;            
        }
        
}

imagethumb("d:/tmp/cbitmap.png",128,"d:/tmp/cbitmap_thumb.jpg");
Saludos