Position actuelle: Accueil> Derniers articles> Comment utiliser la fonction ImageAffineMatrixGet pour implémenter la transformation de rotation d'image? Étapes et exemple explications

Comment utiliser la fonction ImageAffineMatrixGet pour implémenter la transformation de rotation d'image? Étapes et exemple explications

gitbox 2025-07-09

Comment utiliser la fonction ImageAffineMatrixGet pour implémenter la transformation de rotation d'image? Étapes et exemple explications

Le traitement d'image est une tâche courante en PHP, en particulier lors du développement d'un site Web, de l'édition d'une image ou de la génération de miniatures. Pour implémenter la transformation de rotation des images, PHP fournit certaines fonctions et outils. L'une des fonctions très utiles est ImageAffineMatrixGet () , qui est utilisée pour obtenir une matrice de transformation affine. Grâce à cette matrice, vous pouvez faire tourner, mettre à l'échelle, couper et d'autres opérations de transformation sur l'image.

1. Qu'est-ce qu'une matrice de transformation affine?

Dans le traitement d'image, la transformation affine est une transformation qui maintient les lignes parallèles inchangées. La rotation, la traduction, la mise à l'échelle et d'autres opérations sont toutes une transformation affine. La matrice de transformation affine est une matrice 2x3 qui définit comment transformer chaque position de pixels dans une image. Grâce à cette matrice, PHP peut effectuer des transformations géométriques complexes sur les images.

La structure de la matrice de transformation affine est la suivante:

 <span><span>[</span><span><span class="hljs-meta">a, b, c</span></span><span>]
[</span><span><span class="hljs-meta">d, e, f</span></span><span>]
</span></span>
  • A , B , D , E sont des valeurs utilisées pour la mise à l'échelle, la rotation, le cisaillement, etc.

  • C et F sont les valeurs de la transformation de la traduction.

2. La fonction de la fonction ImageAffineMatrixGet ()

La fonction ImageAffineMatrixget () dans PHP est utilisée pour générer une matrice de transformation affine, en particulier la matrice requise pour générer des transformations de rotation. La syntaxe de cette fonction est la suivante:

 <span><span><span class="hljs-title function_ invoke__">imageaffinematrixget</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$type</span></span><span>)
</span></span>

$ type est une chaîne qui spécifie le type de transformation que vous souhaitez générer. Les types de transformation courants sont:

  • «Rotation» : génère une matrice de rotation.

  • «Échelle» : génère la matrice de mise à l'échelle.

  • 'Cish' : générer une matrice de cisaillement.

Pour les opérations de rotation, nous utilisons généralement le type «rotation» .

3. Comment implémenter la transformation de rotation à l'aide d' imageAffineMatrixGet ()

Ci-dessous, nous utiliserons un exemple simple pour montrer comment utiliser la fonction ImageAffineMatrixGet () pour obtenir la rotation de l'image.

Étape 1: Créez une ressource d'image

Tout d'abord, nous devons charger un fichier image, qui peut être dans n'importe quel format pris en charge, comme JPEG ou PNG.

 <span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Chargement des images</span></span><span>
</span><span><span class="hljs-variable">$image</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imagecreatefromjpeg</span></span><span>(</span><span><span class="hljs-string">'example.jpg'</span></span><span>);
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

Étape 2: Obtenez la matrice de rotation

Utilisez la fonction ImageAffineMatrixGet () pour obtenir la matrice de rotation. Supposons que nous voulons faire pivoter l'image de 45 degrés.

 <span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Obtenez la matrice de rotation</span></span><span>
</span><span><span class="hljs-variable">$matrix</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imageaffinematrixget</span></span><span>(</span><span><span class="hljs-string">'rotate'</span></span><span>, </span><span><span class="hljs-number">45</span></span><span>);
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

Étape 3: générer une transformation affine

Ensuite, utilisez la fonction ImageAffine () pour appliquer la matrice à l'image. La fonction ImageAffine () accepte deux paramètres: la ressource d'image et la matrice de transformation affine.

 <span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Appliquer une transformation affine</span></span><span>
</span><span><span class="hljs-variable">$transformed_image</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imageaffine</span></span><span>(</span><span><span class="hljs-variable">$image</span></span><span>, </span><span><span class="hljs-variable">$matrix</span></span><span>);
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

Étape 4: Enregistrer ou sortir l'image transformée

Maintenant, nous pouvons enregistrer l'image transformée en un fichier ou la sortir directement dans le navigateur.

 <span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Enregistrer l&#39;image transformée</span></span><span>
</span><span><span class="hljs-title function_ invoke__">imagejpeg</span></span><span>(</span><span><span class="hljs-variable">$transformed_image</span></span><span>, </span><span><span class="hljs-string">'rotated_example.jpg'</span></span><span>);

</span><span><span class="hljs-comment">// Ou sortir directement vers le navigateur</span></span><span>
</span><span><span class="hljs-title function_ invoke__">header</span></span><span>(</span><span><span class="hljs-string">'Content-Type: image/jpeg'</span></span><span>);
</span><span><span class="hljs-title function_ invoke__">imagejpeg</span></span><span>(</span><span><span class="hljs-variable">$transformed_image</span></span><span>);
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

Étape 5: Ressources gratuites

Une fois le traitement d'image terminé, n'oubliez pas de libérer les ressources d'image.

 <span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Mémoire libre</span></span><span>
</span><span><span class="hljs-title function_ invoke__">imagedestroy</span></span><span>(</span><span><span class="hljs-variable">$image</span></span><span>);
</span><span><span class="hljs-title function_ invoke__">imagedestroy</span></span><span>(</span><span><span class="hljs-variable">$transformed_image</span></span><span>);
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

4. Exemple de code complet

Voici un exemple de code complet, combinant toutes les étapes ci-dessus:

 <span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Chargement des images</span></span><span>
</span><span><span class="hljs-variable">$image</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imagecreatefromjpeg</span></span><span>(</span><span><span class="hljs-string">'example.jpg'</span></span><span>);

</span><span><span class="hljs-comment">// Obtenez la matrice de rotation</span></span><span>
</span><span><span class="hljs-variable">$matrix</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imageaffinematrixget</span></span><span>(</span><span><span class="hljs-string">'rotate'</span></span><span>, </span><span><span class="hljs-number">45</span></span><span>);

</span><span><span class="hljs-comment">// Appliquer une transformation affine</span></span><span>
</span><span><span class="hljs-variable">$transformed_image</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imageaffine</span></span><span>(</span><span><span class="hljs-variable">$image</span></span><span>, </span><span><span class="hljs-variable">$matrix</span></span><span>);

</span><span><span class="hljs-comment">// Enregistrer l&#39;image transformée</span></span><span>
</span><span><span class="hljs-title function_ invoke__">imagejpeg</span></span><span>(</span><span><span class="hljs-variable">$transformed_image</span></span><span>, </span><span><span class="hljs-string">'rotated_example.jpg'</span></span><span>);

</span><span><span class="hljs-comment">// Ou sortir directement vers le navigateur</span></span><span>
</span><span><span class="hljs-title function_ invoke__">header</span></span><span>(</span><span><span class="hljs-string">'Content-Type: image/jpeg'</span></span><span>);
</span><span><span class="hljs-title function_ invoke__">imagejpeg</span></span><span>(</span><span><span class="hljs-variable">$transformed_image</span></span><span>);

</span><span><span class="hljs-comment">// Mémoire libre</span></span><span>
</span><span><span class="hljs-title function_ invoke__">imagedestroy</span></span><span>(</span><span><span class="hljs-variable">$image</span></span><span>);
</span><span><span class="hljs-title function_ invoke__">imagedestroy</span></span><span>(</span><span><span class="hljs-variable">$transformed_image</span></span><span>);
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

5. Conclusion

Grâce à la fonction ImageAffineMatrixGet () , nous pouvons facilement obtenir la matrice requise pour la transformation de rotation et l'appliquer à l'image via la fonction ImageFin () pour réaliser le fonctionnement de rotation de l'image. Cette approche fournit un moyen flexible et efficace de gérer la transformation de l'image, en particulier lorsque un contrôle fin de l'image est nécessaire.

J'espère que cet article peut vous aider à comprendre comment utiliser les fonctions de traitement d'image de PHP pour faire tourner et transformer des images!