10/10/2018, 00:04
Viết văn bản trên file ảnh! bó tay với file png và gif
Mình vừa viết 1 class để xử lý 1 số thao tác trên file ảnh như ghép khung hình. viết text lên file ảnh... nhưng có 1 số vướn mắc như thế này.
+ Nhưng file ảnh dạng gif va png thì ra màu lem lung tung
+ Mình chưa biết cách tạo resource image cho file bmp
*Mình viết bằng ngôn ngữ PHP
+ Nhưng file ảnh dạng gif va png thì ra màu lem lung tung
+ Mình chưa biết cách tạo resource image cho file bmp
*Mình viết bằng ngôn ngữ PHP
Bài liên quan





class image { var $imagesource = null; var $widthimage = 0; var $heightimage = 0; var $error = array(); var $filename = ""; var $colorname = array(); function image($file) { if(empty($file)){ $error[] = "No file found!"; exit(); } $this->imagesource = $this->Createimageresource($file); $this->_Getimagesize(); $this->filename = $this->_Getfilename($file); } function _Getfilename($file) { $pattern = "/([a-zA-Z0-9_@-]*\.[a-zA-Z0-9_@-]{2,4})$/"; preg_match($pattern,$file,$match); return $match[1]; } function Createcolor($colorname,$red = 0, $green = 0,$blue = 0) { $this->colorname[$colorname] = imagecolorallocate($this->imagesource, $red, $green, $blue); } function Writetexttoimage($text='',$textsize = 12,$angeltext = 100,$x = 0,$y =0,$colorname="black",$font = "arial.ttf") { putenv('GDFONTPATH=' . realpath('.')."/fonts"); if($this->imagesource==null) { $this->error[] = "No image resource"; return false; } else imagettftext($this->imagesource,$textsize,$angeltext,$x,$y,$this->colorname[$colorname],$font,$text); //$this->Out(); } function Createimageresource($file) { $ext = $this->_Detectfileimage($file); switch ($ext) { case "jpg": $resourceimage = imagecreatefromjpeg($file); break; case "gif": $resourceimage = imagecreatefromgif($file); break; case "png": $resourceimage = imagecreatefrompng($file); break; default: $resourceimage = imagecreatefromjpeg($file); break; } return $resourceimage; } function _Detectfileimage($image) { $pattern = "#.*\.([a-zA-Z0-9_]{2,4})$#"; preg_match($pattern,$image,$match); return $match[1]; } function _Getimagesize() { $imagesize = array(); if (is_null($resource)) { $this->widthimage = imagesx($this->imagesource); $this->heightimage = imagesy($this->imagesource); } else { $this->widthimage = imagesx($resource); $this->heightimage = imagesy($resource); } } function Out() { $imagetype = $this->_Detectfileimage($this->filename); header("Content-type: image/$imagetype"); switch ($imagetype) { case "jpg": { imagejpeg($this->imagesource); } break; case "png": { imagepng($this->imagesource); } case "gif": { imagegif($this->imagesource); } break; default: { imagejpeg($this->imagesource); } break; } } function saveimage($save_dir = "download/") { $imagetype = $this->_Detectfileimage($this->filename); switch ($imagetype) { case "jpg": { imagejpeg($this->imagesource,$save_dir.$this->filename); } break; case "png": { imagepng($this->imagesource,$save_dir.$this->filename); } case "gif": { imagegif($this->imagesource,$save_dir.$this->filename); } break; default: { imagejpeg($this->imagesource,$save_dir.$this->filename); } break; } } function imagemerge($fileimagedes,$des_x,$des_y,$source_x,$source_y,$source_w=0,$source_h=0,$pct) { if($des_x<0) { $des_x = $this->widthimage+$des_x; } if($des_y <0) { $des_y = $this->heightimage+$des_y; } if($source_x<0) { $source_x = $this->widthimage+$source_x; } if($source_y<0) { $source_y = $this->heightimage+$source_y; } if($source_w==0) { $source_w=$this->widthimage; } if($source_h==0) { $source_h=$this->heightimage; } if($source_w<0) { $source_w=$this->widthimage+$source_w; } if($source_h<0) { $source_h=$this->heightimage+$source_h; } $des_image_source = $this->Createimageresource($fileimagedes); imagecopymerge($this->imagesource,$des_image_source,$des_x,$des_y,$source_x,$source_y,$source_w,$source_h,$pct); } function imagedestroy() { imagedestroy($this->imagesource); } }