Meteoric Stream - Original Apps & Music, Piano Instrumental, Children music, Hymns


Japanese | English
Meteoric Stream -> Documentation -> PHP&MySQL -> [PHP] Return as a variable of raw data with resize from image file

[PHP] Return as a variable of raw data with resize from image file

This is the function of return a variable of raw data with resize from image file that loading from JPEG, PNG, or GIF.

Firstly, loads image file of JPEG/PNG/GIF,
And resize it,
And return as a variable.

It's able to set to $_SESSION when you use base64_encode, something like this, 

$_SESSION["image_data"] = base64_encode(img_data_resize($file_name, 600, 400));

function img_data_resize($file_name, $dest_w = 0, $dest_h = 0, $quality_jpg = 90, $quality_png = 6){ list($src_w, $src_h, $type) = getimagesize($file_name); switch($type){ case IMAGETYPE_JPEG: $src_img = imagecreatefromjpeg($file_name); break; case IMAGETYPE_PNG: $src_img = imagecreatefrompng($file_name); break; case IMAGETYPE_GIF: $src_img = imagecreatefromgif($file_name); break; default: return ""; } if($dest_w == 0 && $dest_h == 0){ $dest_w = $src_w; $dest_h = $src_h; }else{ if($dest_h == 0){ @$dest_h = (int)($dest_w / $src_w * $src_h); }else{ if($dest_w == 0){ @$dest_w = (int)($dest_h / $src_h * $src_w); }else{ @$dest_w_tmp = (int)($dest_h / $src_h * $src_w); @$dest_h_tmp = (int)($dest_w_tmp / $src_w * $src_h); if($dest_w_tmp > $dest_w){ @$dest_h_tmp = (int)($dest_w / $dest_w_tmp * $dest_h_tmp); @$dest_w_tmp = $dest_w; } if($dest_h_tmp > $dest_h){ @$dest_w_tmp = (int)($dest_h / $dest_h_tmp * $dest_w_tmp); @$dest_h_tmp = $dest_h; } $dest_w = $dest_w_tmp; $dest_h = $dest_h_tmp; } } } $dest_img = imagecreatetruecolor($dest_w, $dest_h); imagecopyresampled($dest_img, $src_img, 0,0,0,0, $dest_w, $dest_h, $src_w, $src_h); ob_start(); switch($type){ case IMAGETYPE_JPEG: imagejpeg($dest_img, null, $quality_jpg); break; case IMAGETYPE_PNG: imagepng($dest_img, null, $quality_png); break; case IMAGETYPE_GIF: imagegif($dest_img, null); break; } $data = ob_get_contents(); ob_end_clean(); imagedestroy($src_img); imagedestroy($dest_img); return $data; }

Lastupdated:2017/11/15
Wrote by:2017/09/07

Share this article

Related











Go To Documentation Top
Meteoric Stream -> Documentation -> PHP&MySQL -> [PHP] Return as a variable of raw data with resize from image file
Go to Top