X
pm studio world wide news pj club
pm studio world wide news pj club
news, tech, music, film, sports, cg, translation, photo uploader
  • 2016-12-24
  • PJ Club
  • How To Resize and Compress Image with PHP
  • Sometime I want to resize the pictures or compress data, such as JPEG, PNG and GIF. But once posted data to the server, the repost is too much effort. So, I have made the function for resize or compression with PHP on the server.
    Be careful for PNG file, because PNG can include alpha channel. If you want to to keep alpha channel, you have to write below.

         imagealphablending(resource $image, false);
         imagesavealpha(resource $image, true);

    Function name is "resizeImage( )". $fileName is what image file name you want to resize. $new_width is how much width of image you want to resize. $directory is where you want to save file is.
    For example, you have PNG file called "demo.png", which is 1024 x 680 pixels with alpha channel. If you want to resize width 700 pixels and save it to another directory (this time "new"), you will write script like below. (You have to put PHP file at the same directory with PNG file.)
  • <?php

         $fileName="demo.png";
         $new_width=700;
         $directory= "new/".$fileName;
         resizeImage($fileName,$new_width,$directory);

         function resizeImage($fileName,$new_width,$directory){
              list($width,$height,$type) = getimagesize($fileName);
              $new_height = round($height*$new_width/$width);
              $old_image = imagecreatetruecolor($new_width,$new_height);
              switch($type){
                  case IMAGETYPE_JPEG:
                      $new_image = imagecreatefromjpeg($fileName);
                      break;
                  case IMAGETYPE_GIF:
                      $new_image = imagecreatefromgif($fileName);
                      break;
                  case IMAGETYPE_PNG:
                      imagealphablending($old_image, false);
                      imagesavealpha($old_image, true);
                      $new_image = imagecreatefrompng($fileName);
                      break;
              }
              switch($type){
                  case IMAGETYPE_JPEG:
                      imagecopyresampled($old_image,$new_image,0,0,0,0,$new_width,$new_height,$width,$height);
                      imagejpeg($old_image,$directory);
                      break;
                  case IMAGETYPE_GIF:
                      $bgcolor = imagecolorallocatealpha($new_image,0,0,0,127);
                      imagefill($old_image, 0, 0, $bgcolor);
                      imagecolortransparent($old_image,$bgcolor);
                      imagecopyresampled($old_image,$new_image,0,0,0,0,$new_width,$new_height,$width,$height);
                      imagegif($old_image,$directory);
                      break;
                  case IMAGETYPE_PNG:
                      imagecopyresampled($old_image,$new_image,0,0,0,0,$new_width,$new_height,$width,$height);
                      imagepng($old_image,$directory);
                      break;
              }
              imagedestroy($old_image);
              imagedestroy($new_image);
         }

    ?>

    Next case is compression data. You can compress data with PHP, but JPEG and PNG have different to set compression number method.
    In the case of JPEG, large number means best quality. The default is about 75 value.
    JPEG :
    Quality: from 0 (worst quality, smaller file) to 100 (best quality, biggest file).
    Meanwhile, PNG is opposite JPEG, the value means compression level.
    PNG :
    Compression level: from 0 (no compression) to 9.

    If you want to compress data, you will add number of compression level or quality value for creating image file function.

    JPEG :
         imagejpeg($old_image,$directory, 75);
    PNG :
         imagepng($old_image,$directory,9);

    After making function, I wanted to change file type from PNG with alpha channel to JPEG. But PHP outputted JPEG file with black color on alpha channel. So, I tryed to replace from black color to white color.
    At first, you don't keep alpha channel, because, if you keep alpha channel, maybe you find out rough edge on image. Script is below.

         imagealphablending(resource $image, ture);
         imagesavealpha(resource $image, false);

    Next step, you add and blend white color on alpha channel. Script is below.

         $whitecolor =imagecolorallocate($new_image, 255, 255, 255);
         imagefill($old_image, 0, 0, $whitecolor);
         imagecopyresampled($old_image,$new_image,0,0,0,0,$new_width,$new_height,$width,$height);
         imagejpeg($old_image,$directory);

    Sometime, you might have problem "time out" when you run script. A solution is that you write uni_set ( ) on the first line of script.
    For example, you can have three minutes during script running with script below.

         ini_set("max_execution_time",180);

    The whole script is below.
  • <?php

         ini_set("max_execution_time",180);
         $fileName="demo.png";
         $jpegName="demo.jpg";
         $new_width=700;
         $directory= "new/".$jpegName;
         resizeImage($fileName,$new_width,$directory);

         function resizeImage($fileName,$new_width,$directory){
              list($width,$height,$type) = getimagesize($fileName);
              $new_height = round($height*$new_width/$width);
              $old_image = imagecreatetruecolor($new_width,$new_height);
              switch($type){
                  case IMAGETYPE_JPEG:
                      $new_image = imagecreatefromjpeg($fileName);
                      break;
                  case IMAGETYPE_GIF:
                      $new_image = imagecreatefromgif($fileName);
                      break;
                  case IMAGETYPE_PNG:
                      imagealphablending($old_image, true);
                      imagesavealpha($old_image, false);
                      $new_image = imagecreatefrompng($fileName);
                      break;
              }
              switch($type){
                  case IMAGETYPE_JPEG:
                      imagecopyresampled($old_image,$new_image,0,0,0,0,$new_width,$new_height,$width,$height);
                      imagejpeg($old_image,$directory);
                      break;
                  case IMAGETYPE_GIF:
                      $bgcolor = imagecolorallocatealpha($new_image,0,0,0,127);
                      imagefill($old_image, 0, 0, $bgcolor);
                      imagecolortransparent($old_image,$bgcolor);
                      imagecopyresampled($old_image,$new_image,0,0,0,0,$new_width,$new_height,$width,$height);
                      imagegif($old_image,$directory);
                      break;
                  case IMAGETYPE_PNG:
                      $whitecolor =imagecolorallocate($new_image, 255, 255, 255);
                      imagefill($old_image, 0, 0, $whitecolor);
                      imagecopyresampled($old_image,$new_image,0,0,0,0,$new_width,$new_height,$width,$height);
                      imagejpeg($old_image,$directory);
                      break;
              }
              imagedestroy($old_image);
              imagedestroy($new_image);
         }

    ?>
  • source : PHP Net
Recent Post
Latest News
Our Product
PMS Translate
PMS Translateは、どこのWebページからでも選択した英文・和文を簡単に英訳・和訳する自動翻訳システムです。
「PMS Translate」ボタンをお気に入りバーへドラッグして、追加して頂くとご使用いただけます。

詳細はこちら
Our Product
PMS CG Animation
PMS CG Animationは、3DソフトウエアMAYAで制作したCG作品集です。
2Dと3Dをあわせたアニメーションやキャラクターのテストアニメーションをご覧頂けます。

詳細はこちら
  • Editor: Toshio Maeoka / pm studio
    pm studio world wide news © 2014-2017 . All Rights Reserved
^
to TOP