Невозможно написать на изображении после рисования водяного знака с помощью GD
Моя проблема заключается в следующем:
я могу без проблем писать на картинке
Но когда я рисую водяной знак [на картинке]
My Text Apper Но со 100% размытием
я не знаю что случилось??
я приложу изображение после того, как я сохранил его
он содержит текст до того, как я нарисую [ Welcome ] черным цветом, а текст после рисования водяного знака [ To php ] синим
Учебный класс
class image {
public $image = '' ; // Image as [ File || Url ]
protected $_image = '' ;
public $resource ; // Image Resource
protected $info = array() ; // Image Info And Check If Image
protected $resize = array() ;
/*
* array ( width , height )
*/
protected $crop = array() ;
/*
* array( left , right , width , height )
*/
protected $rotate = 90 ;
/*
* Intgre dgree
*/
protected $text_info = array() ;
/*
* array(
* 'text' => 'Hello World' ,
* 'font' => 'fonts/arial.ttf' ,
* 'color' => '#ff0000' ,
* 'size' => 20 ,
* 'angle' => 0 ,
* 'position' => 'TopCenter'
* )
*/
protected $image_info = array() ;
/*
* array(
* 'image' => 'logo.jpg' ,
* 'opacity' => 20 ,
* 'position' => 'TopCenter'
* )
*/
public function __construct($image = ''){
if( $image != '' ) {
$this -> SetImage($image) ;
$this -> _image = $image ;
}
return $this ;
}
public function __restart(){
if( $image != '' ) {
$this -> SetImage($this->_image) ;
}
return $this ;
}
public function SetImage ( $image ){
if( isset($image{4}) ) {
$this -> image = $image ;
$this -> info = @getimagesize( $image ) ;
if( empty( $this -> info ) ) {
trigger_error( 'Sorry Not Vaild Image' ) ; die() ;
}
$this->info['ext'] = '.'.str_replace( 'image/' , '' , $this -> info['mime'] ) ;
$this -> resource() ;
}else{
trigger_error( 'Can You Please Select Image' ) ; die() ;
}
return $this ;
}
public function SetReize( $array = array() ) {
if( is_array( $array ) && !empty( $array ) ) {
$this -> resize = $array ;
} else {
trigger_error( 'Sorry Resize Info Should Be Numeric array' ) ; die() ;
}
return $this ;
}
public function SetCrop( $array = array() ) {
if( is_array( $array ) && !empty( $array ) ) {
$this -> crop = $array ;
} else {
trigger_error( 'Sorry Crop Info Should Be Numeric array' ) ; die() ;
}
return $this ;
}
public function SetRotate( $rot = 90 ) {
if( (int) $rot > 0 ) {
$this -> rotate = $rot ;
} else {
trigger_error( 'Sorry Rotate Info Should Be Numeric Bigger Than 0' ) ; die() ;
}
return $this ;
}
public function Text( $text = 'Hello World' , $font = 'fonts/arial.ttf' , $color = 'ff0000' , $size = 20 , $angle = 0 , $position = 'TopCenter') {
$this -> text_info = array(
'text' => $text ,
'font' => $font ,
'color' => $color ,
'size' => $size ,
'angle' => $angle ,
'position' => $position
) ;
return $this ;
}
public function Image( $image = 'logo.png' , $opacity = '50' , $position = 'TopCenter') {
$this -> image_info = array(
'image' => $image ,
'opacity' => $opacity ,
'position' => $position
) ;
return $this ;
}
private function text_pos(){
/*
if( preg_match("/^[\x0600-\x06FF]/i", $this -> text_info['text']) ) {
$this -> text_info['text'] = $this -> revUni ( iconv( iconv_get_encoding($this -> text_info['text']),'UTF-8', $this -> text_info['text'] ) ) ;
}
*/
$text = imagettfbbox( $this -> text_info['size'] , $this -> text_info['angle'] , $this -> text_info['font'] , $this -> text_info['text'] ) ;
$text[5] = $text[7] = -1 * $text[5] ;
return $this->get_position( $this -> text_info['position'], $text['4'] , $text['5'] ) ;
}
private function image_pos($info = array()){
if( !is_array( $info ) or empty( $info ) ) {
$info = @getimagesize( $this -> image_info['image'] ) ;
}
return $this->get_position( $this -> image_info['position'], $info['0'] , $info['1'] ) ;
}
private function get_position( $pos = 'CenterCenter' , $width = 100 , $height = 100 ) {
$src_width = $this->info[0] ;
$src_height = $this->info[1] ;
if( $pos == 'TopLeft' ) {
return array( 'x' => 0 , 'y' => 0 , 0 , 0 ) ;
}elseif( $pos == 'TopCenter' ){
$x = (int) (($src_width - $width) / 2) ;
return array( 'x' => $x , 'y' => $height , $x , $height ) ;
}elseif( $pos == 'TopRight' ){
$x = (int) ($src_width - $width) ;
return array( 'x' => $x , 'y' => $height , $x , $height ) ;
}elseif( $pos == 'CenterLeft' ){
$x = (int) (($src_width - $width) / 2) ;
$y = (int) (($src_height - $height) / 2) ;
return array( 'x' => 0 , 'y' => $y , 0 , $y ) ;
}elseif( $pos == 'CenterCenter' ){
$x = (int) (($src_width - $width) / 2) ;
$y = (int) (($src_height - $height) / 2) ;
return array( 'x' => $x , 'y' => $y , $x , $y ) ;
}elseif( $pos == 'CenterRight' ){
$x = (int) ($src_width - $width) ;
$y = (int) (($src_height - $height) / 2) ;
return array( 'x' => $x , 'y' => $y , $x , $y ) ;
}elseif( $pos == 'BottomLeft' ){
$x = 0 ;
$y = (int) ($src_height - $height) ;
return array( 'x' => $x , 'y' => $y , $x , $y ) ;
}elseif( $pos == 'BottomCenter' ){
$x = (int) (($src_width - $width) /2) ;
$y = (int) ($src_height - $height) ;
return array( 'x' => $x , 'y' => $y , $x , $y ) ;
}elseif( $pos == 'BottomRight' ){
$x = (int) ($src_width - $width) ;
$y = (int) ($src_height - $height) ;
return array( 'x' => $x , 'y' => $y , $x , $y ) ;
}elseif( is_array($pos) and count($pos) == 2 ){
$x = (int) $pos[0] ;
$y = (int) $pos[1] ;
return array( 'x' => $x , 'y' => $y , $x , $y ) ;
}else{ //
$x = (int) (($src_width - $width) / 2) ;
$y = (int) (($src_height - $height) / 2) ;
return array( 'x' => $x , 'y' => $y , $x , $y ) ;
}
}
private function rgb2hex2rgb($c) {
if(!$c) return false;
$c = trim($c);
$out = false;
if(preg_match("/^[0-9ABCDEFabcdef\#]+$/i", $c)){
$c = str_replace('#','', $c);
$l = strlen($c) == 3 ? 1 : (strlen($c) == 6 ? 2 : false);
if($l){
unset($out);
$out[0] = $out['r'] = $out['red'] = hexdec(substr($c, 0,1*$l));
$out[1] = $out['g'] = $out['green'] = hexdec(substr($c, 1*$l,1*$l));
$out[2] = $out['b'] = $out['blue'] = hexdec(substr($c, 2*$l,1*$l));
}else $out = false;
}elseif (preg_match("/^[0-9]+(,| |.)+[0-9]+(,| |.)+[0-9]+$/i", $c)){
$spr = str_replace(array(',',' ','.'), ':', $c);
$e = explode(":", $spr);
if(count($e) != 3) return false;
$out = '#';
for($i = 0; $i<3; $i++)
$e[$i] = dechex(($e[$i] <= 0)?0:(($e[$i] >= 255)?255:$e[$i]));
for($i = 0; $i<3; $i++)
$out .= ((strlen($e[$i]) < 2)?'0':'').$e[$i];
$out = strtoupper($out);
}else $out = false;
return $out;
}
private function resource(){
$mime = $this -> info['mime'] ;
if( $mime == 'image/jpeg' || $mime == 'image/jpg' ) {
$this -> resource = imagecreatefromjpeg( $this->image ) ;
}elseif( $mime == 'image/gif' ) {
$this -> resource = imagecreatefromgif( $this->image ) ;
}elseif( $mime == 'image/png' ) {
$this -> resource = imagecreatefrompng( $this->image ) ;
}else{
trigger_error( 'UnDefiended Image MimeType' ) ; die() ;
}
}
private function revUni($text) {
$wordsArray = explode(" ", $text);
$rtlCompleteText='';
for ($i = sizeOf($wordsArray); $i > -1; $i = $i-1) {
$lettersArray = explode(";", $wordsArray[$i]);
$rtlWord='';
for ($k = sizeOf($lettersArray); $k > -1; $k = $k-1) {
if (strlen($lettersArray[$k]) > 1) {
$rtlWord = $rtlWord."".$lettersArray[$k].";";
}
}
$rtlCompleteText = $rtlCompleteText." ".$rtlWord;
}
return $rtlCompleteText;
}
public function resize(){
if( !empty( $this -> resize ) ){
$res = $this -> resize ;
$wid = $res[0] ;
$hei = $res[1] ;
$mime = $this -> info['mime'] ;
$tmp = $this -> unique().$this -> info['ext'] ;
$this -> save( $tmp ) ;
$__ = $this -> image ;
//$this -> image = $tmp ; $this -> resource() ;
$this -> SetImage ( $tmp ) ;
if($mime == 'image/png')
{
$thumb_ = imagecreatefrompng($tmp) ;
}
elseif($mime == 'image/gif')
{
$thumb_ = imagecreatefromgif($tmp) ;
}
else
{
$thumb_ = imagecreatefromjpeg($tmp) ;
}
$thumb = imagecreatetruecolor($wid,$hei) ;
imagecopyresampled($thumb,$thumb_, 0, 0, 0, 0,$wid,$hei,$this -> info[0],$this -> info[1]);
$this -> resource = $thumb ;
imagedestroy($thumb_) ;
//$this -> save($this -> unique().$this -> info['ext']) ;
unlink($tmp) ;
//$this -> SetImage ( $__ ) ;
return $this ;
}
}
public function crop()
{
if( !empty( $this -> crop ) ){
$res = $this -> crop ;
$wid = $res[0] ;
$hei = $res[1] ;
$lef = $res[2] ;
$rig = $res[3] ;
$mime = $this -> info['mime'] ;
$tmp = $this -> unique().$this -> info['ext'] ;
$this -> save( $tmp ) ;
$__ = $this -> image ;
$this -> SetImage ( $tmp ) ;
if($mime == 'image/png')
{
$crop_src = imagecreatefrompng($tmp) ;
}
elseif($mime == 'image/gif')
{
$crop_src = imagecreatefromgif($tmp) ;
}
else
{
$crop_src = imagecreatefromjpeg($tmp) ;
}
}
$crop = imagecreatetruecolor($wid,$hei) ;
imagecopy($crop,$crop_src, 0, 0,$lef,$rig,$this -> info[0],$this -> info[1]);
$this->resource = $crop ;
imagedestroy($crop_src) ;
//$this -> save($this -> unique().$this -> info['ext']) ;
unlink($tmp) ;
return $this ;
}
public function rotate() {
$rotate = imagerotate($this->resource, $this -> rotate , -1) ;
$this -> resource = $rotate ;
return $this ;
}
public function write(){
$pos = $this -> text_pos() ;
$colo = $this -> rgb2hex2rgb($this->text_info['color']) ;
$color = imagecolorallocate( $this->resource , $colo[0], $colo[1], $colo[2]);
imagettftext($this->resource, $this->text_info['size'], $this->text_info['angle'], $pos['x'], $pos['y'], $color , $this->text_info['font'],$this->text_info['text']);
return $this ;
}
public function draw()
{
if(!file_exists($this->image_info['image']))
die('You WaterMark Image Not Found') ;
$vaild = @getimagesize( $this->image_info['image'] ) ;
if( empty( $vaild ) ) {
trigger_error( 'Sorry Not Vaild Image' ) ; die() ;
}
if( $vaild['mime'] == 'image/jpeg' ) {
$water_ = imagecreatefromjpeg($this->image_info['image']) ;
}elseif( $vaild['mime'] == 'image/gif' ) {
$water_ = imagecreatefromgif($this->image_info['image']) ;
}elseif( $vaild['mime'] == 'image/png' ) {
$water_ = imagecreatefrompng($this->image_info['image']) ;
}else{
trigger_error( 'Logo Must be [ .jpg - Png - Gif ]' ) ; die() ;
}
$thu_wid = $vaild[0] ;
$thu_hei = $vaild[1] ;
$pos = $this -> image_pos( array( $thu_wid , $thu_hei ) ) ;
// print_r( $pos ) ; die() ;
imagealphablending($this->resource,false) ;
imagesavealpha($this->resource, true) ;
imagecopymerge($this->resource,$water_,$pos['x'],$pos['y'],0,0,$thu_wid,$thu_hei,$this->image_info['opacity']) ;
imagedestroy($water_) ;
return $this ;
}
protected function unique($len = 10){
return substr( md5(uniqid().time()) , 0 , $len ) ;
}
public function save( $new_name = '' ) {
if( $new_name == '' ) {
$new_name = $this -> unique() . $this -> info['ext'];
}
$mime = $this -> info['mime'] ;
if($mime == 'image/gif')
{
imagegif($this->resource, $new_name) ;
}
elseif($mime == 'image/png')
{
imagepng($this->resource, $new_name) ;
}
else
{
imagejpeg($this->resource,$new_name) ;
}
}
public function show() {
$mime = $this -> info['mime'] ;
header( 'Content-type:'.$mime ) ;
if($mime == 'image/gif')
{
imagegif($this->resource) ;
}
elseif($mime == 'image/png')
{
imagepng($this->resource) ;
}
else
{
imagejpeg($this->resource) ;
}
}
}
И мой код
$image = new image( 'Chrysanthemum.jpg' ) ;
$image -> SetReize( array( 250,250 ) ) ;
$image -> SetRotate( 90 ) ;
$image -> SetCrop ( array( 250,250,0,0 ) ) ;
$image -> Text( 'Welcome' , 'Arial.ttf' , '#000000' , 40 , 0 , 'TopCenter' ) ;
$image -> Image( 'logo.jpg' , 50 , 'BottomLeft' ) ;
$image -> write() -> draw() ;
$image -> Text( 'To PHP' , 'Arial.ttf' , '#0000ff' , 40 , 0 , 'TopRight' ) ;
$image -> Image( 'logo.jpg' , 50 , 'BottomRight' ) ;
$image -> write() -> draw() -> save() ;