how to prevent imagecreatefrom from creating a static gif - PHP

Viewed 170

I have a script that sends an image it is saved as a .gif as declared in the movie.php file that will be listed below. However, as I understand it, it creates an image from the gif making it useless for display because it simply becomes a static image .gif. Anyway I wanted to know how I can upload this file and ignore this function (imagecreatefromgif) I tried to change it in several ways and when I remove it I get an error, someone could help me work around this so that the gif will be sent and not be converted to a gif file static. basically I wanted that the way I sent the gif it would only be renamed with function imageGenerateName () but that it would keep all its size and property. Every help is welcome. Thanks in advance . Movie.php code:

<?php

  class Movie {

    public function imageGenerateName() {
      return bin2hex(random_bytes(60)) . ".gif";
    }

  }

movie-process.php code

<?php
          // Upload  img
          if(isset($_FILES["image"]) && !empty($_FILES["image"]["tmp_name"])) {
    
            $image = $_FILES["image"];
            $imageTypes = ["image/gif"];
            $jpgArray = ["image/gif"];
    
            // Check img type
            if(in_array($image["type"], $imageTypes)) {
    
              // Check img type
              if(in_array($image["type"], $jpgArray)) {
                $imageFile = imagecreatefromgif($image["tmp_name"]);
              } else {
                $imageFile = imagecreatefromgif($image["tmp_name"]);
              }   
              // image name
              $imageName = $movie->imageGenerateName();
    
              imagegif($imageFile, "./img/movies/" . $imageName, 100);
    
              $movie->image = $imageName;
    
            } 
          }  
              // Upload img
              if(isset($_FILES["image"]) && !empty($_FILES["image"]["tmp_name"])) {
    
                $image = $_FILES["image"];
                $imageTypes = ["image/gif"];
                $jpgArray = ["image/gif"];
    
                // Check img type
                if(in_array($image["type"], $imageTypes)) {
    
                  // check type is gif
                  if(in_array($image["type"], $jpgArray)) {
                    $imageFile = imagecreatefromgif($image["tmp_name"]);
                  }
    
                  // generete img name
                  $movie = new Movie();
    
                  $imageName = $movie->imageGenerateName();
    
                  imagegif($imageFile, "./img/movies/" . $imageName, 100);
    
                  $movieData->image = $imageName;   
                }
            }   
2 Answers

Unfortunately, the imagecreatefromgif function will only read the first image of the gif as the PHP manual says.

I tried before to solve this, but there is no turnaround other than uploading the image without touching it. PHP uses the GD library and this library doesn't have the proper functionality to deal with gif images other than just saving the image with the *.gif extension.

So, this is my suggested solution:

// generete img name
$movie = new Movie();
$imageName = $movie->imageGenerateName();
move_uploaded_file($_FILES["image"]["tmp_name"],"./img/movies/" . $imageName);
$movieData->image = $imageName; 

I think since you say you just want to save it as it is, with the same size and its all properties, you can use below code

<?php

    $allowed = array('gif');
    $filename = $_FILES["image"]['name'];
    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    if (in_array($ext, $allowed)) {
    
                    // generete img name
                    $movie = new Movie();
                    $imageName = $movie->imageGenerateName();

          $uploadResult = move_uploaded_file($_FILES['image']['tmp_name'], dirname( dirname( __FILE__ ) ).'/img/movies/'. $imageName );
                    
            if($uploadResult === true ){
                    $movie->image = $imageName;    
            }else{
                throw new \Exception('Unable to copy file to the given path');
            }
    }

Or if you have access to the Imagick lib php extension refer to below link on how to install this php-extension

https://www.php.net/manual/en/imagick.setup.php

then using the functions in that library you can do

<?php 

        $allowed = array('gif');
        $filename = $_FILES["image"]['name'];
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if (in_array($ext, $allowed)) {

$image = new Imagick(); 
$image->readImage($_FILES["image"]["tmp_name"]);

$image = $image->coalesceImages(); 

foreach ($image as $frame) { 
  $frame->cropImage($crop_w, $crop_h, $crop_x, $crop_y); 
  $frame->thumbnailImage($size_w, $size_h); 
  $frame->setImagePage($size_w, $size_h, 0, 0); 
} 

$image = $image->deconstructImages(); 
$image->writeImages(dirname( dirname( __FILE__ ) ).'/img/movies/'. $imageName, true); 

}
?>
Related