2009-10-30 6 views
4

Je souhaite incliner une image en une forme trapézoïdale. Les bords gauche et droit doivent être droits de haut en bas; les bords supérieur et gauche doivent être angulaires. Je n'ai aucune idée de la meilleure façon de le faire. J'utilise GD Library et PHP. Est-ce que quelqu'un peut-il me montrer la bonne direction?Comment incliner une image avec la bibliothèque GD?

Merci, Jason

Répondre

5

Essayez ceci:

<? 
// Set it up 
$img_name = "grid.jpg"; 
$src_img = imagecreatefromjpeg($img_name); 
$magnify = 4; 

// Magnify the size 
$w = imagesx($src_img); 
$h = imagesy($src_img); 
$dst_img = imagecreatetruecolor($w * $magnify, $h * $magnify); 
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $w * $magnify, $h * $magnify, $w, $h); 
$src_img = $dst_img; 

// Skew it 
$w *= $magnify; 
$h *= $magnify; 
$new_lh = abs($h * 0.66); 
$new_rh = $h ; 
$step = abs((($new_rh - $new_lh)/2)/$w); 
$from_top = ($new_rh - $new_lh)/2 ; 
$dst_img = imagecreatetruecolor($w, $new_rh); 
$bg_colour = imagecolorallocate($dst_img, 255, 255, 255); 
imagefill($dst_img, 0, 0, $bg_colour); 
for ($i = 0 ; $i < $w ; $i ++) 
{ 
    imagecopyresampled($dst_img, $src_img, $i, $from_top - $step * $i, $i, 0, 1, $new_lh + $step * $i * 2, 1, $h); 
} 

// Reduce the size to "anti-alias" it 
$src_img = $dst_img; 
$dst_img = imagecreatetruecolor($w/$magnify * 0.85, $new_rh/$magnify); 
imagecopyresampled ($dst_img, $src_img, 0, 0, 0, 0, $w/$magnify * 0.85, $h/$magnify, $w, $h); 

header("Content-Type: image/jpg"); 
imagejpeg($dst_img); 
?> 
+0

merci pour la solution. avez-vous pour l'autre côté? – lilsizzo

2

Je trouve ce thread (Néerlandais traduit -> anglais) discuter de la même chose. On dirait que c'est peut-être ce que tu cherches. Je pense qu'il est clair que vous ne pouvez pas biaiser avec GD sans écrire votre propre fonction pour le faire. Si vous avez ImageMagick disponible, vous pouvez trouver ceci à easier to achieve.

Bonne chance.

+0

Oui, ImageMagick est ce qui est venu à mon esprit, il peut faire plus rien. – Don

Questions connexes