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
I would like to resample and make the image transparent. Could you please write me a brief code?
Regards
ASKER
Can you please write me why following code is not working?
Regards
<?php
function addtext($text,$maxwidth,$f ontsize,$f ont){
$text_array=array();
$size = ImageTTFBBox ($fontsize, 0, $font, $text);
$width = $size[2]-$size[0];
if ($width < $maxwidth) {
array_push($text_array,$te xt);
} else {
$spaces = explode(" ",$text);
$temptext = "";
for($n=0;$n<count($spaces) ;$n++){
$oldtext = $temptext;
$temptext = trim($temptext." ".$spaces[$n]);
$count=substr_count($tempt ext,"<BR>" );
if ($count>0)
{
$pos=strpos($temptext,"<BR >");
$part=substr($temptext,$po s);
$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_st ring);
$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,tri m($previou s));
array_push($text_array,tri m(str_repl ace($previ ous,"",$te mptext)));
$oldtext = "";
break;
}
else {
$previous=$temp;
$temp=$temp." ".$part_string[$k];
}
}
}
else {
array_push($text_array,tri m($temptex t));
}
$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,tri m($oldtext ));
$temptext = $spaces[$n];
$oldtext = "";
} else {
for($i=0;$i<strlen($tempte xt);$i++){
$size = ImageTTFBBox ($fontsize, 0, $font,substr($temptext,0,$ i+1));
$width = $size[2]-$size[0];
if ($width > $maxwidth) {
array_push($text_array,sub str($tempt ext,0,$i)) ;
$temptext = substr($temptext,$i);
addtext($temptext,$maxwidt h,$fontsiz e,$font);
$temptext = "";
break;
}
}
}
}
}
if ($temptext <> "") {
//addtext($temptext,$maxwi dth,$fonts ize,$font) ;
array_push($text_array,$te mptext);
}
}
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_widt h)/2;
$new_bbox2=($width-$headin g_width_bb ox2)/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($f ill_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));
?>
Regards
<?php
function addtext($text,$maxwidth,$f
$text_array=array();
$size = ImageTTFBBox ($fontsize, 0, $font, $text);
$width = $size[2]-$size[0];
if ($width < $maxwidth) {
array_push($text_array,$te
} else {
$spaces = explode(" ",$text);
$temptext = "";
for($n=0;$n<count($spaces)
$oldtext = $temptext;
$temptext = trim($temptext." ".$spaces[$n]);
$count=substr_count($tempt
if ($count>0)
{
$pos=strpos($temptext,"<BR
$part=substr($temptext,$po
$temptext=substr($temptext
$part=str_replace("<BR>","
$oldtext = $temptext;
$size = ImageTTFBBox ($fontsize, 0, $font, $temptext);
$width = $size[2]-$size[0];
if ($width > $maxwidth) {
$part_string=explode(" ",$temptext);
$part_count=count($part_st
$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,tri
array_push($text_array,tri
$oldtext = "";
break;
}
else {
$previous=$temp;
$temp=$temp." ".$part_string[$k];
}
}
}
else {
array_push($text_array,tri
}
$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,tri
$temptext = $spaces[$n];
$oldtext = "";
} else {
for($i=0;$i<strlen($tempte
$size = ImageTTFBBox ($fontsize, 0, $font,substr($temptext,0,$
$width = $size[2]-$size[0];
if ($width > $maxwidth) {
array_push($text_array,sub
$temptext = substr($temptext,$i);
addtext($temptext,$maxwidt
$temptext = "";
break;
}
}
}
}
}
if ($temptext <> "") {
//addtext($temptext,$maxwi
array_push($text_array,$te
}
}
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_widt
$new_bbox2=($width-$headin
$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($f
$fill = imagecolorallocate($im, $colour->red, $colour->green, $colour->blue);
//echo $fill."<br>";
//imagefill( $im, 1, 1, $fill );
//imagecolortransparent( $im, $fill );
// Resample
imagecopyresampled($image_
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
?>
that's another question...
ASKER
No it's not bugada. That code that you showed me is too long. Could you please check my one?
Thanks
Thanks
Try this code is slighy modified. I cannot check if work beacuse my server hasn't gd library.
<?php
function addtext($text,$maxwidth,$f ontsize,$f ont){
$text_array=array();
$size = ImageTTFBBox ($fontsize, 0, $font, $text);
$width = $size[2]-$size[0];
if ($width < $maxwidth) {
array_push($text_array,$te xt);
} else {
$spaces = explode(" ",$text);
$temptext = "";
for($n=0;$n<count($spaces) ;$n++){
$oldtext = $temptext;
$temptext = trim($temptext." ".$spaces[$n]);
$count=substr_count($tempt ext,"<BR>" );
if ($count>0)
{
$pos=strpos($temptext,"<BR >");
$part=substr($temptext,$po s);
$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_st ring);
$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,tri m($previou s));
array_push($text_array,tri m(str_repl ace($previ ous,"",$te mptext)));
$oldtext = "";
break;
}
else {
$previous=$temp;
$temp=$temp." ".$part_string[$k];
}
}
}
else {
array_push($text_array,tri m($temptex t));
}
$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,tri m($oldtext ));
$temptext = $spaces[$n];
$oldtext = "";
} else {
for($i=0;$i<strlen($tempte xt);$i++){
$size = ImageTTFBBox ($fontsize, 0, $font,substr($temptext,0,$ i+1));
$width = $size[2]-$size[0];
if ($width > $maxwidth) {
array_push($text_array,sub str($tempt ext,0,$i)) ;
$temptext = substr($temptext,$i);
addtext($temptext,$maxwidt h,$fontsiz e,$font);
$temptext = "";
break;
}
}
}
}
}
if ($temptext <> "") {
//addtext($temptext,$maxwi dth,$fonts ize,$font) ;
array_push($text_array,$te mptext);
}
}
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_widt h)/2;
$new_bbox2=($width-$headin g_width_bb ox2)/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));
?>
<?php
function addtext($text,$maxwidth,$f
$text_array=array();
$size = ImageTTFBBox ($fontsize, 0, $font, $text);
$width = $size[2]-$size[0];
if ($width < $maxwidth) {
array_push($text_array,$te
} else {
$spaces = explode(" ",$text);
$temptext = "";
for($n=0;$n<count($spaces)
$oldtext = $temptext;
$temptext = trim($temptext." ".$spaces[$n]);
$count=substr_count($tempt
if ($count>0)
{
$pos=strpos($temptext,"<BR
$part=substr($temptext,$po
$temptext=substr($temptext
$part=str_replace("<BR>","
$oldtext = $temptext;
$size = ImageTTFBBox ($fontsize, 0, $font, $temptext);
$width = $size[2]-$size[0];
if ($width > $maxwidth) {
$part_string=explode(" ",$temptext);
$part_count=count($part_st
$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,tri
array_push($text_array,tri
$oldtext = "";
break;
}
else {
$previous=$temp;
$temp=$temp." ".$part_string[$k];
}
}
}
else {
array_push($text_array,tri
}
$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,tri
$temptext = $spaces[$n];
$oldtext = "";
} else {
for($i=0;$i<strlen($tempte
$size = ImageTTFBBox ($fontsize, 0, $font,substr($temptext,0,$
$width = $size[2]-$size[0];
if ($width > $maxwidth) {
array_push($text_array,sub
$temptext = substr($temptext,$i);
addtext($temptext,$maxwidt
$temptext = "";
break;
}
}
}
}
}
if ($temptext <> "") {
//addtext($temptext,$maxwi
array_push($text_array,$te
}
}
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_widt
$new_bbox2=($width-$headin
$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_
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
?>
Only a thing: remember that if you are using gd library v1.6+ you haven't anymore imagegif(), so you may use imagepng()
ASKER
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);
Regards
imagecolortransparent( $im, $fill );
$image_p = imagecreatetruecolor($max_
imagecopyresized($image_p,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
No bugada, it still looks awful. Do you have any other solution?
Cheers
Cheers
ASKER
I am using small fonts and small images, so I need something regarding that.
Thanks
Thanks
http://pupius.co.uk/download/php_scripts/preview/class_imageManipulator.phps/