Link to home
Start Free TrialLog in
Avatar of hasozduru
hasozduru

asked on

Resample and make transparent or vice versa

Hi

I would like to resample and make the image transparent. Could you please write me a brief code?

Regards
Avatar of bugada
bugada
Flag of Italy image

Avatar of hasozduru
hasozduru

ASKER

Can you please write me why following code is not working?

Regards


<?php
function addtext($text,$maxwidth,$fontsize,$font){
      $text_array=array();
      $size = ImageTTFBBox ($fontsize, 0, $font, $text);
        $width = $size[2]-$size[0];
        if ($width < $maxwidth) {
              array_push($text_array,$text);
        } else {
            $spaces = explode(" ",$text);
            $temptext = "";
            for($n=0;$n<count($spaces);$n++){
                  $oldtext = $temptext;
                  $temptext = trim($temptext." ".$spaces[$n]);                  
                  $count=substr_count($temptext,"<BR>");
                  
                  if ($count>0)                   
                  {      
                        $pos=strpos($temptext,"<BR>");
                        $part=substr($temptext,$pos);
                        $temptext=substr($temptext,0,$pos);
                        
                        $part=str_replace("<BR>","",$part);
                        $oldtext = $temptext;
                        $size = ImageTTFBBox ($fontsize, 0, $font, $temptext);
                        $width = $size[2]-$size[0];
                        if ($width > $maxwidth) {
                              $part_string=explode(" ",$temptext);
                              $part_count=count($part_string);
                              $temp=$part_string[0];
                              $previous="";
                              for ($k=1;$k<=$part_count;$k++) {
                                    $size = ImageTTFBBox ($fontsize, 0, $font, $temp);
                                    $width = $size[2]-$size[0];
                                    if ($width>$maxwidth) {
                                          array_push($text_array,trim($previous));
                                          array_push($text_array,trim(str_replace($previous,"",$temptext)));
                                          $oldtext = "";
                                          break;
                                    }
                                    else {
                                           $previous=$temp;
                                           $temp=$temp." ".$part_string[$k];
                                     }
                              }
                        }
                        else {
                              array_push($text_array,trim($temptext));                                    
                        }
                        $temptext=$part;
                        for ($j=1;$j<$count;$j++) {
                              array_push($text_array,"");
                        }
                        continue;
                  }
                  
                    $size = ImageTTFBBox ($fontsize, 0, $font, $temptext);
                  $width = $size[2]-$size[0];
                  if ($width > $maxwidth) {
                        if ($oldtext <> ""){
                              array_push($text_array,trim($oldtext));
                              $temptext = $spaces[$n];
                              $oldtext = "";
                        } else {
                               for($i=0;$i<strlen($temptext);$i++){
                                     $size = ImageTTFBBox ($fontsize, 0, $font,substr($temptext,0,$i+1));
                                    $width = $size[2]-$size[0];
                                    if ($width > $maxwidth) {
                                          array_push($text_array,substr($temptext,0,$i));
                                          $temptext = substr($temptext,$i);
                                          addtext($temptext,$maxwidth,$fontsize,$font);
                                          $temptext = "";
                                          break;
                                     }
                               }            
                        }      
                  }
            }
            if ($temptext <> "") {
                  //addtext($temptext,$maxwidth,$fontsize,$font);
                  array_push($text_array,$temptext);                                          
            }      
        }
        return $text_array;
}

function createImage($heading, $font_size_heading, $sub_heading, $font_size_sub_heading) {
      $font_heading = 'lte50697.ttf';
      $font_sub_heading = 'lte50703.ttf';
      // Set the content-type
    header("Content-type: image/gif");
      
      // Create the image
      $im = imagecreate(505*2, 100*2);
      
      // Create some colors
      $black = imagecolorallocate($im, 0, 0, 0);
      //$red = imagecolorallocate($im, 255, 0, 0);
      //$grey = imagecolorallocate($im, 128, 128, 128);
      $white = imagecolorallocate($im, 255, 255, 255);
      //imagefill( $im, 1, 1, $red );
      
      if(count($heading) == 1){
        $indent = 90;
      }
      else {
      $indent = 60;
      }
      $i=0;
      $temp = $font_size_heading * 1.5;
      
      $indent_bottom = imagettfbbox ($font_size_sub_heading, 0, $font_sub_heading, $sub_heading);
      
      foreach($heading as $value) {
            $bbox=imagettfbbox ($font_size_heading, 0, $font_heading, $value);
            $bbox2=imagettfbbox ($font_size_sub_heading, 0, $font_sub_heading, $sub_heading);
            
            $heading_width = $bbox[2];
            $heading_height = $bbox[3];
          
          $heading_width_bbox2 = $bbox2[2];
            $heading_height_bbox2 = $bbox2[3];
          
          //check width of the image
            $width=imagesx($im);
            
            //calculate x coordinates for text
            $new=($width-$heading_width)/2;
            
            $new_bbox2=($width-$heading_width_bbox2)/2;
            
            $write_indent = $indent+$i*$temp;
      
             imagettftext($im, $font_size_heading, 0, $new, $write_indent, $white, $font_heading, $value);
             imagecolortransparent( $im, $fill );  
          $i++;
      }

      $indent_down = 230-$indent;
      imagettftext($im, $font_size_sub_heading, 0, $new_bbox2, $indent_down, $white, $font_sub_heading, $sub_heading);
      
      //imagettftext($im, $font_size_heading, 0, $new, 30*2, $white, $font_heading, $heading);
      
      $image_p = imagecreate(505*2, 100*2);
      
      $colorObj = new ColorCode();
      //$fill_colour = "0000FF";

      $colour = $colorObj->GetRGBvalues($fill_colour);
      
      $fill = imagecolorallocate($im, $colour->red, $colour->green, $colour->blue);
      //echo $fill."<br>";
      //imagefill( $im, 1, 1, $fill );
      //imagecolortransparent( $im, $fill );
      
      // Resample
      imagecopyresampled($image_p, $im, 0, 0, 0, 0, 505, 100, 505*2, 100*2);
      
      return $image_p;
}

class ColorCode{
      private $red;
      private $green;
      private $blue;

      function GetRGBvalues($colorCode)
      {
        $colorObj->red = intval(substr($colorCode, 0, 2), 16);
        $colorObj->green = intval(substr($colorCode, 2, 2), 16);
        $colorObj->blue = intval(substr($colorCode, 4, 2), 16);
        return $colorObj;
      }
}

$text_array = addtext("hd jd asdf fds safd sdf sads aasdf", 350, 20, 'lte50697.ttf');
imagegif(createImage($text_array, 42, "SUB HEADING OF IMAGE as dsa sdfafds", 10*2));
?>
that's another question...
No it's not bugada. That code that you showed me is too long. Could you please check my one?

Thanks
Try this code is slighy modified. I cannot check if work beacuse my server hasn't gd library.

<?php
function addtext($text,$maxwidth,$fontsize,$font){
     $text_array=array();
     $size = ImageTTFBBox ($fontsize, 0, $font, $text);
       $width = $size[2]-$size[0];
       if ($width < $maxwidth) {
            array_push($text_array,$text);
       } else {
          $spaces = explode(" ",$text);
          $temptext = "";
          for($n=0;$n<count($spaces);$n++){
               $oldtext = $temptext;
               $temptext = trim($temptext." ".$spaces[$n]);              
               $count=substr_count($temptext,"<BR>");
               
               if ($count>0)                
               {    
                    $pos=strpos($temptext,"<BR>");
                    $part=substr($temptext,$pos);
                    $temptext=substr($temptext,0,$pos);
                   
                    $part=str_replace("<BR>","",$part);
                    $oldtext = $temptext;
                    $size = ImageTTFBBox ($fontsize, 0, $font, $temptext);
                    $width = $size[2]-$size[0];
                    if ($width > $maxwidth) {
                         $part_string=explode(" ",$temptext);
                         $part_count=count($part_string);
                         $temp=$part_string[0];
                         $previous="";
                         for ($k=1;$k<=$part_count;$k++) {
                              $size = ImageTTFBBox ($fontsize, 0, $font, $temp);
                              $width = $size[2]-$size[0];
                              if ($width>$maxwidth) {
                                   array_push($text_array,trim($previous));
                                   array_push($text_array,trim(str_replace($previous,"",$temptext)));
                                   $oldtext = "";
                                   break;
                              }
                              else {
                                    $previous=$temp;
                                    $temp=$temp." ".$part_string[$k];
                               }
                         }
                    }
                    else {
                         array_push($text_array,trim($temptext));                              
                    }
                    $temptext=$part;
                    for ($j=1;$j<$count;$j++) {
                         array_push($text_array,"");
                    }
                    continue;
               }
               
                 $size = ImageTTFBBox ($fontsize, 0, $font, $temptext);
               $width = $size[2]-$size[0];
               if ($width > $maxwidth) {
                    if ($oldtext <> ""){
                         array_push($text_array,trim($oldtext));
                         $temptext = $spaces[$n];
                         $oldtext = "";
                    } else {
                          for($i=0;$i<strlen($temptext);$i++){
                               $size = ImageTTFBBox ($fontsize, 0, $font,substr($temptext,0,$i+1));
                              $width = $size[2]-$size[0];
                              if ($width > $maxwidth) {
                                   array_push($text_array,substr($temptext,0,$i));
                                   $temptext = substr($temptext,$i);
                                   addtext($temptext,$maxwidth,$fontsize,$font);
                                   $temptext = "";
                                   break;
                               }
                          }          
                    }    
               }
          }
          if ($temptext <> "") {
               //addtext($temptext,$maxwidth,$fontsize,$font);
               array_push($text_array,$temptext);                                  
          }    
       }
       return $text_array;
}

function createImage($heading, $font_size_heading, $sub_heading, $font_size_sub_heading) {
     $font_heading = 'lte50697.ttf';
     $font_sub_heading = 'lte50703.ttf';
     // Set the content-type
    header("Content-type: image/gif");
     
     // Create the image
     $im = imagecreate(505*2, 100*2);
     
     // Create some colors
     $black = imagecolorallocate($im, 0, 0, 0);
     //$red = imagecolorallocate($im, 255, 0, 0);
     //$grey = imagecolorallocate($im, 128, 128, 128);
     $white = imagecolorallocate($im, 255, 255, 255);
     //imagefill( $im, 1, 1, $red );
     
     if(count($heading) == 1){
       $indent = 90;
     }
     else {
      $indent = 60;
     }
     $i=0;
     $temp = $font_size_heading * 1.5;
     
     $indent_bottom = imagettfbbox ($font_size_sub_heading, 0, $font_sub_heading, $sub_heading);
     
     foreach($heading as $value) {
          $bbox=imagettfbbox ($font_size_heading, 0, $font_heading, $value);
          $bbox2=imagettfbbox ($font_size_sub_heading, 0, $font_sub_heading, $sub_heading);
         
          $heading_width = $bbox[2];
          $heading_height = $bbox[3];
         
         $heading_width_bbox2 = $bbox2[2];
          $heading_height_bbox2 = $bbox2[3];
         
         //check width of the image
          $width=imagesx($im);
         
          //calculate x coordinates for text
          $new=($width-$heading_width)/2;
         
          $new_bbox2=($width-$heading_width_bbox2)/2;
         
          $write_indent = $indent+$i*$temp;
     
            imagettftext($im, $font_size_heading, 0, $new, $write_indent, $white, $font_heading, $value);
            imagecolortransparent( $im, $fill );  
         $i++;
     }

     $indent_down = 230-$indent;
     imagettftext($im, $font_size_sub_heading, 0, $new_bbox2, $indent_down, $white, $font_sub_heading, $sub_heading);
     
     //imagettftext($im, $font_size_heading, 0, $new, 30*2, $white, $font_heading, $heading);
     
     $image_p = imagecreate(505*2, 100*2);
     
     //$colorObj = new ColorCode();
     //$fill_colour = "0000FF";

     $colour = GetRGBvalues($fill_colour);
     
     $fill = imagecolorallocate($im, $colour['red'], $colour['green'], $colour['blue']);
     //echo $fill."<br>";
     //imagefill( $im, 1, 1, $fill );
     //imagecolortransparent( $im, $fill );
     
     // Resample
     imagecopyresampled($image_p, $im, 0, 0, 0, 0, 505, 100, 505*2, 100*2);
     
     return $image_p;
}

 function GetRGBvalues($colorCode)
 {
      $colorObj = array();
    $colorObj['red'] = intval(substr($colorCode, 0, 2), 16);
    $colorObj['green'] = intval(substr($colorCode, 2, 2), 16);
    $colorObj['blue'] = intval(substr($colorCode, 4, 2), 16);
    return $colorObj;
 }

$text_array = addtext("hd jd asdf fds safd sdf sads aasdf", 350, 20, 'lte50697.ttf');
imagegif(createImage($text_array, 42, "SUB HEADING OF IMAGE as dsa sdfafds", 10*2));
?>
Only a thing: remember that if you are using gd library v1.6+ you haven't anymore imagegif(), so you may use imagepng()
It didn't work bugada. I am using this one, why it does not work? I want to minimize my image to half size.

Regards

imagecolortransparent( $im, $fill );
$image_p = imagecreatetruecolor($max_box_width / 2, $max_box_height / 2);
imagecopyresized($image_p, $im, 0, 0, 0, 0, $max_box_width / 2, $max_box_height / 2, $max_box_width, $max_box_height);
ASKER CERTIFIED SOLUTION
Avatar of bugada
bugada
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No bugada, it still looks awful. Do you have any other solution?

Cheers
I am using small fonts and small images, so I need something regarding that.

Thanks