Scritto da davidonzo il 10/09/2008, in OpenSource, Web, Tutorial
ATTENZIONE!
L'articolo che stai leggendo è stato scritto 1242 giorni orsono.
Le informazioni presenti potrebbero non essere aggiornate!

Molti di voi si saranno accorti che la libreria GD scaricata dai repository di ubuntu è compilata a metà. Per motivi di sicurezza (ma non spiegano quali) certe funzioni nella GD distribuita da Canonical non sono abilitate. Una di queste è imagerotate().

Avete a questo punto due possibilità. La prima è quella classica: scaricate i sorgenti dal sito ufficiale e compilate la gd, oppure, se non siete molto avvezzi all'uso avanzato di linux, potete rimpiazzare la funzione del core con queste righe di codice.

<?
// $src_img - a GD image resource
// $angle - degrees to rotate clockwise, in degrees
// returns a GD image resource
// USAGE:
// $im = imagecreatefrompng('test.png');
// $im = imagerotate($im, 15);
// header('Content-type: image/png');
// imagepng($im);
function imageRotateBicubic($src_img, $angle, $bicubic=false) {
  
    // convert degrees to radians
    $angle = $angle + 180;
    $angle = deg2rad($angle);
  
    $src_x = imagesx($src_img);
    $src_y = imagesy($src_img);
  
    $center_x = floor($src_x/2);
    $center_y = floor($src_y/2);
  
    $rotate = imagecreatetruecolor($src_x, $src_y);
    imagealphablending($rotate, false);
    imagesavealpha($rotate, true);

    $cosangle = cos($angle);
    $sinangle = sin($angle);
  
    for ($y = 0; $y < $src_y; $y++) {
      for ($x = 0; $x < $src_x; $x++) {
    // rotate...
    $old_x = (($center_x-$x) * $cosangle + ($center_y-$y) * $sinangle)
      + $center_x;
    $old_y = (($center_y-$y) * $cosangle - ($center_x-$x) * $sinangle)
      + $center_y;
  
    if ( $old_x >= 0 && $old_x < $src_x
         && $old_y >= 0 && $old_y < $src_y ) {
      if ($bicubic == true) {
        $sY  = $old_y + 1;
        $siY  = $old_y;
        $siY2 = $old_y - 1;
        $sX  = $old_x + 1;
        $siX  = $old_x;
        $siX2 = $old_x - 1;
      
        $c1 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY2));
        $c2 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY));
        $c3 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY2));
        $c4 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY));
      
        $r = ($c1['red']  + $c2['red']  + $c3['red']  + $c4['red']  ) << 14;
        $g = ($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) << 6;
        $b = ($c1['blue']  + $c2['blue']  + $c3['blue']  + $c4['blue'] ) >> 2;
        $a = ($c1['alpha']  + $c2['alpha']  + $c3['alpha']  + $c4['alpha'] ) >> 2;
        $color = imagecolorallocatealpha($src_img, $r,$g,$b,$a);
      } else {
        $color = imagecolorat($src_img, $old_x, $old_y);
      }
    } else {
          // this line sets the background colour
      $color = imagecolorallocatealpha($src_img, 255, 255, 255, 127);
    }
    imagesetpixel($rotate, $x, $y, $color);
      }
    }
    return $rotate;
}
?>

Funzione prelevata fra i contributi dei lettori, nella pagina del manuale dedicata alla funzione.

Hai trovato l'articolo interessante?
Sottoscrivi il Feed RSS per essere informato automaticamente degli ultimi aggiornamenti!
 
.Commenti rss
# 1
If you want to get your own function back you can also recompile the gd.so library by following these simple steps:

http://info.momeunier.fr/node/20
Di Marcool  (Inviato il 16/10/2008 @ 15:31:24)
# 2
what is "blabla" on that link?
Di matt  (Inviato il 19/03/2009 @ 22:48:20)
# 3
blabla is a typical italian slang to say "foo" or "foobar", but where did you read it?
Di davidonzo  (Inviato il 19/03/2009 @ 23:17:01)
# 4
Thanks for the manual function...
Di sutriadi  (Inviato il 21/09/2009 @ 23:36:54)
# 5
The function works except it is cropping out part of the image when I use it,
for example if the image is 96x110 before rotating, then it is still 96x110 after even though the picture in the image is rotated. it should be 110x96 afterwards.
I just pasted it into my application but did not change anything except:
changed from
function imageRotateBicubic(
to
function imageRotate(
so it would be compatible with my existing script.
Di jbrown  (Inviato il 29/10/2009 @ 21:15:43)


I commenti possono essere moderati.
Se non lo vedi comparire subito non reinserirlo più volte.
Grazie per la gentile collaborazione.