title: How to split "non-animated and animated" pictures, for correctly use resize function
body: <p>With this script I can upload multiple images with animations or without.
  Included many functions: resize, watermark, correct orientation, send data to ajax, etc...</p>
  <pre><code>&lt;?php
  define('FORUM_PATH', '/.../');
  require_once (FORUM_PATH . '.../login.php');
  $ipbMemberLoginApi = new apiMemberLogin();
  $ipbMemberLoginApi-&gt;init();
  $member = $ipbMemberLoginApi-&gt;getMember();
  $id = ($member['member_id']);
  $countFiles = count($_FILES['files']['name']);
  $upload_location = &quot;uploads/$id/&quot; . date(&quot;ymd&quot;) . &quot;/&quot;;
  if (!is_dir($upload_location)) mkdir($upload_location, 0755, true);
  $chars = 'aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789';
  $files_arr = array();
  for ($i = 0;$i &lt; $countFiles;$i++) {
      $fileName = $_FILES['files']['name'][$i];
      $ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
      $valid_ext = array(&quot;png&quot;, &quot;jpeg&quot;, &quot;jpg&quot;, &quot;webp&quot;, &quot;gif&quot;, &quot;bmp&quot;);
      if (in_array($ext, $valid_ext)) {
          $imgName = $chars[mt_rand(1, 62) ] . substr(str_shuffle(uniqid()), 0, 6) . '.' . $ext;
          $path = $upload_location . $imgName;
          if (move_uploaded_file($_FILES['files']['tmp_name'][$i], $path)) {
              $abort = false;
              $im = new Imagick();
              try {
                  $im-&gt;pingImage($path);
              }
              catch(ImagickException $e) {
                  unlink($path);
                  $files_arr['BadIMG'][] = $fileName;
                  $abort = true;
              }
              if (!$abort) {
                  if ($ext == &quot;gif&quot;) {
                      $file = file_get_contents($path);
                      $animated = preg_match('#(\x00\x21\xF9\x04.{4}\x00\x2C.*){2,}#s', $file);
                  }
                  if ($animated != 1) {
                      //For non-animated pictures
                      $image = new Imagick($path);
                      $props = $image-&gt;getImageProperties('exif:Orientation', true);
                      $orientation = isset($props['exif:Orientation']) ? $props['exif:Orientation'] : null;
                      if ($orientation != 0) {
                          switch ($image-&gt;getImageOrientation()) {
                              case Imagick::ORIENTATION_TOPLEFT:
                                  break;
                              case Imagick::ORIENTATION_TOPRIGHT:
                                  $image-&gt;flopImage();
                                  break;
                              case Imagick::ORIENTATION_BOTTOMRIGHT:
                                  $image-&gt;rotateImage(&quot;#000&quot;, 180);
                                  break;
                              case Imagick::ORIENTATION_BOTTOMLEFT:
                                  $image-&gt;flopImage();
                                  $image-&gt;rotateImage(&quot;#000&quot;, 180);
                                  break;
                              case Imagick::ORIENTATION_LEFTTOP:
                                  $image-&gt;flopImage();
                                  $image-&gt;rotateImage(&quot;#000&quot;, -90);
                                  break;
                              case Imagick::ORIENTATION_RIGHTTOP:
                                  $image-&gt;rotateImage(&quot;#000&quot;, 90);
                                  break;
                              case Imagick::ORIENTATION_RIGHTBOTTOM:
                                  $image-&gt;flopImage();
                                  $image-&gt;rotateImage(&quot;#000&quot;, 90);
                                  break;
                              case Imagick::ORIENTATION_LEFTBOTTOM:
                                  $image-&gt;rotateImage(&quot;#000&quot;, -90);
                                  break;
                              default:
                                  break;
                          }
                          $image-&gt;setImageOrientation(imagick::ORIENTATION_TOPLEFT);
                          $image-&gt;writeImage($path);
                      }
                      $im-&gt;readImage($path);
                      $max_width = 1024;
                      $max_height = 768;
                      $im-&gt;stripImage();
                      $im-&gt;resizeImage(min($im-&gt;getImageWidth(), $max_width), min($im-&gt;getImageHeight(), $max_height), imagick::FILTER_CATROM, 1, true);
                      $imWidth = $im-&gt;getImageWidth();
                      $imHeight = $im-&gt;getImageHeight();
                      if ($imWidth &lt; 150 || $imHeight &lt; 150) {
                          $watermark = new Imagick(&quot;noWatermark.png&quot;);
                      } elseif ($imWidth &lt; 401 || $imHeight &lt; 401) {
                          $watermark = new Imagick(&quot;watermark_small.png&quot;);
                      } else {
                          $watermark = new Imagick(&quot;watermark.png&quot;);
                      }
                      $margin_right = 2;
                      $margin_bottom = 2;
                      $x = $im-&gt;getImageWidth() - $watermark-&gt;getImageWidth() - $margin_right;
                      $y = $im-&gt;getImageHeight() - $watermark-&gt;getImageHeight() - $margin_bottom;
                      $im-&gt;compositeImage($watermark, Imagick::COMPOSITE_OVER, $x, $y);
                      $watermark-&gt;destroy();
                      file_put_contents($path, $im);
                      $files_arr['GoodIMG'][] = str_replace('uploads/', '', $path);
                  } else {
                      //For animated pictures
                      system(&quot;convert $path -coalesce -repage 0x0 -scale 800x\&gt; -layers Optimize $path&quot;);
                      $imGif = new Imagick($path);
                      $max_size = 180;
                      $imGifWidth = $imGif-&gt;getImageWidth();
                      $imGifHeight = $imGif-&gt;getImageHeight();
                      if ($imGifWidth || $imGifHeight &gt; 180) {
                          system(&quot;convert $path -coalesce null: watermark.png -gravity SouthEast -geometry +0+0 -layers composite -layers optimize $path&quot;);
                      }
                      $files_arr['GoodIMG'][] = str_replace('uploads/', '', $path);
                  }
              }
          }
      }
  }
  header('Content-Type: application/json');
  echo json_encode($files_arr);
  die;
  </code></pre>
  <p>PHP Version 5.6.40</p>
  <p>But have problem:
  If I upload <strong>($animated == 1).GIF</strong> together with <strong>($animated != 1)</strong>, this picture <strong>($animated != 1)</strong> goes in function for resize animated pictures.</p>
  <p>How to split animated picture from non-animated picture correctly if I upload together?</p>
  <p><strong>Added more details:</strong></p>
  <p>If try upload:</p>
  <ul>
  <li>AAA.gif - animated</li>
  <li>BBB.jpeg - non-animated</li>
  </ul>
  <p><strong>BBB</strong>.jpeg goes in function for resize animated pictures.</p>
  <p>But, if try upload:</p>
  <ul>
  <li>AAA.jpeg - non-animated</li>
  <li>BBB.gif - animated</li>
  </ul>
  <p><strong>AAA</strong>.jpeg goes in function for resize non-animated pictures. This is a correct.</p>
  <hr />
  <p><strong>Solved</strong></p>
  <pre><code>        if ($ext == &quot;gif&quot;) {
              $file = file_get_contents($path);
              $animated = preg_match('#(\x00\x21\xF9\x04.{4}\x00\x2C.*){2,}#s', $file);
          } else {
              $animated = 0;
          }
  </code></pre>