본문 바로가기
코딩과 알고리즘

php - resize image function

php - resize image function

Resizing image is frequent skill for PHP web.
Usally it is used for making thumbnail image.

but a lot of resizing source is rightless I think,
so I am making this source :)

function resize_image($file, $newfile, $w, $h) {
   list($width, $height) = getimagesize($file);
   if(strpos(strtolower($file), ".jpg"))
      $src = imagecreatefromjpeg($file);
   else if(strpos(strtolower($file), ".png"))
      $src = imagecreatefrompng($file);
   else if(strpos(strtolower($file), ".gif"))
      $src = imagecreatefromgif($file);
   $dst = imagecreatetruecolor($w, $h);
   imagecopyresampled($dst, $src, 0, 0, 0, 0, $w, $h, $width, $height);
   if(strpos(strtolower($newfile), ".jpg"))
      imagejpeg($dst, $newfile);
   else if(strpos(strtolower($newfile), ".png"))
      imagepng($dst, $newfile);
   else if(strpos(strtolower($newfile), ".gif"))
      imagegif($dst, $newfile);

}

You just focus on filename.
When filename is given extension '.jpg', '.png', '.gif',
this can read and make thumbnail type of that extension file perfectly.

usage)
resize_image("original filename", "destination filename", width, height);

example)

resize_image("org/model.jpg", "new/model200.jpg", 200, 100);  // make jpg file
resize_image("org/model.jpg", "new/model200.png", 200, 100);  // make png file
resize_image("org/model.jpg", "new/model200.gif", 200, 100);  // make gif file

When use make thumbnail, you give a read and write permission to folder.

linux shell, type this command

> chmod 766 target_folder

Thank you read this context.
I hope you will be successed.