PHP Implementation Image Rotation Effect Example Code

  • 2021-07-21 08:05:09
  • OfStack

Rotation of Image by PHP


<div>
    <h4> Before rotation </h4>
    <img src="1.png" style="border:1px solid red;">
  </div>
  <?php
  header("content-type","text/html;charset=utf-8");
   
  /*
  * Picture edge y Axial rotation , With png Format as an example 
  *@param string $filename  Pictorial url
  */
  function turn_y($filename)
  {
    /* Create a picture resource */
    $backy = imagecreatefrompng($filename);
  
    /* Get size */
    $widthy = imagesx($backy);
    $heighty = imagesy($backy);
  
    /* Create a new picture resource and save the flipped picture */
    $newy = imagecreatetruecolor($widthy, $heighty);
  
    /* Along y The axis is turned over, that is, the original drawing is pressed from right to left 1 Pixel width is copied one by one into the new resource */
    for ($i=0; $i < $widthy; $i++) { 
      imagecopy($newy,$backy,$widthy-$i-1,0,$i,0,1,$heighty);
    }
  
    /* Save the flipped picture */
    imagepng($newy,'test3.png');
  
    /* Release resources */
    imagedestroy($backy);
    imagedestroy($newy);
  }
  
  /*
  * Picture edge x Axial rotation , With png Format as an example 
  *@param string $filename  Pictorial url
  */
  function turn_x($filename)
  {
    /* Create a picture resource */
    $backx = imagecreatefrompng($filename);
  
    /* Get size */
    $widthx = imagesx($backx);
    $heightx = imagesy($backx);
  
    /* Create a new picture resource and save the flipped picture */
    $newx = imagecreatetruecolor($widthx, $heightx);
  
    /* Along x The axis is turned over, that is, the original drawing is pressed from top to bottom 1 Pixel width is copied one by one into the new resource */
    for ($i=0; $i < $heightx; $i++) { 
      imagecopy($newx,$backx,0,$heightx-$i-1,0,$i,$widthx,1);
    }
  
    /* Save the flipped picture */
    imagepng($newx,'test4.png');
  
    /* Release resources */
    imagedestroy($backx);
    imagedestroy($newx);
  }
  /* Call function */
  turn_y('1.png');
  turn_x('1.png');
  ?>
  <div style="float:left">
    <h4> Along y Axial rotation </h4>
    <img src="test3.png" style="border:1px solid red;">
  </div>
  <div style="float:left">
    <h4> Along x Axial rotation </h4>
    <img src="test4.png" style="border:1px solid red;">
  </div>

Related articles: