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.
'코드이그나이터와 php7와 mysql' 카테고리의 다른 글
코드이그나이터 뷰를 나눠볼까요? (2) | 2019.10.12 |
---|---|
코드이그나이터 뷰의 파라미터 전달 (0) | 2019.09.27 |
php - 이미지 리사이즈(크기변경) (2) | 2019.09.17 |
php, 네임스페이스 [ namespace ] ?! (0) | 2019.09.14 |
코드이그나이터4의 URL 규칙 (0) | 2019.09.07 |