matthewst Posted September 28, 2009 Share Posted September 28, 2009 What I'm trying to do: ------------------------------------- resized | white background image | and text here here | ------------------------------------- What I currently have: ------------------------------------- image here | all white not resized | no text and looks | displays like a | negative text | is cut off ------------------------------------- Here is my code: <?php header("Content-type: image/jpg"); $insert = imagecreatefromjpeg("image.jpg"); $im = imagecreate(1300, 800); $background = imagecolorallocatealpha($im, 255, 255, 255, 127); imagecolortransparent($insert,imagecolorat($insert,50,50)); $insert_x = imagesx($insert); $insert_y = imagesy($insert); $text = "blah blah blah blah"; $text_color = imagecolorallocate($im, 233, 14, 91); imagecopymerge($im,$insert,0,0,0,0,$insert_x,$insert_y,100); imagestring($im, 8, 1150, 300, $text, $text_color); imagejpeg($im,"",100); ?> Quote Link to comment https://forums.phpfreaks.com/topic/175813-solved-trying-to-add-white-background-and-text-to-image-with-gd/ Share on other sites More sharing options...
matthewst Posted September 28, 2009 Author Share Posted September 28, 2009 Got the image resizer to work by including and external script but now my text doesn't show up at all. <?php header("Content-type: image/jpg"); include('imgsize.php'); $insert = imagecreatefromjpeg(resizeImage('image.jpg','','650')); $im = imagecreate(1300, 800); $background = imagecolorallocatealpha($im, 255, 255, 255, 127); imagecolortransparent($insert,imagecolorat($insert,50,50)); $insert_x = imagesx($insert); $insert_y = imagesy($insert); $text = "blah blah blah blah"; $text_color = imagecolorallocate($im, 233, 14, 91); imagecopymerge($im,$insert,0,0,0,0,$insert_x,$insert_y,100); imagestring($im, 8, 1150, 300, $text, $text_color); imagejpeg($im,"",100); ?> imagesize.php: <?php // Image Resize Script by Zipline Interactive // http://www.gozipline.com // http://www.gozipline.com/42,phpimageresizefunction // info@gozipline.com // 02/02/2008 // This will resize an image keeping its height/width aspect ratio if you do not specify a height value. // USAGE: // MAINTAIN ASPECT AND SAVE FILE. // This will create a new files called 'resizedImage.jpg' with a longest side resized to 600px. // resizeImage('../uploads/uploadedImage.jpg','resizedImage.jpg','600'); // MATIAN ASPECT AND SHOW THE IMAGE IN THE BROWSER. // No other output can be shown before this image. // resizeImage('../images/uploadedImage.jpg','','450'); // RESHAPE IMAGE WITH SET SIZES // This will reshape the image to the set dimensions and save as a jpg file. // resizeImage('uploadedImage.jpg','../images/resized/resizedImage.jpg','150','150'); function resizeImage($source, $destination = NULL,$wdt, $height = NULL){ if(empty($height)){ // Height is nit set so we are keeping the same aspect ratio. list($width, $height) = getimagesize($source); if($width > $height){ $w = $wdt; $h = ($height / $width) * $w; $w = $w; }else{ $w = $wdt; $h = $w; $w = ($width / $height) * $w; } }else{ // Both width and Height are set. // this will reshape to the new sizes. $w = $wdt; $h = $height; } $source_image = @file_get_contents($source) or die('Could not open'.$source); $source_image = @imagecreatefromstring($source_image) or die($source.' is not a valid image'); $sw = imagesx($source_image); $sh = imagesy($source_image); $ar = $sw/$sh; $tar = $w/$h; if($ar >= $tar){ $x1 = round(($sw - ($sw * ($tar/$ar)))/2); $x2 = round($sw * ($tar/$ar)); $y1 = 0; $y2 = $sh; }else{ $x1 = 0; $y1 = 0; $x2 = $sw; $y2 = round($sw/$tar); } $slate = @imagecreatetruecolor($w, $h) or die('Invalid thumbnail dimmensions'); imagecopyresampled($slate, $source_image, 0, 0, $x1, $y1, $w, $h, $x2, $y2); // If $destination is not set this will output the raw image to the browser and not save the file if(!$destination) header('Content-type: image/jpeg'); @imagejpeg($slate, $destination, 75) or die('Directory permission problem'); ImageDestroy($slate); ImageDestroy($source_image); if(!$destination) exit; return true; } ?> Any Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/175813-solved-trying-to-add-white-background-and-text-to-image-with-gd/#findComment-926518 Share on other sites More sharing options...
matthewst Posted September 28, 2009 Author Share Posted September 28, 2009 Am I in the right area? Quote Link to comment https://forums.phpfreaks.com/topic/175813-solved-trying-to-add-white-background-and-text-to-image-with-gd/#findComment-926584 Share on other sites More sharing options...
matthewst Posted September 29, 2009 Author Share Posted September 29, 2009 Some one here has to have had some experience with gd. Quote Link to comment https://forums.phpfreaks.com/topic/175813-solved-trying-to-add-white-background-and-text-to-image-with-gd/#findComment-926966 Share on other sites More sharing options...
matthewst Posted September 29, 2009 Author Share Posted September 29, 2009 Okay, I've been able to increase the canvas size which allowed me to place text outside the image. Now I'm trying to increase the text size as well as wrap it. Any time I try to do anything with imagettftext the page just displays the url. No image, no errors. Just http://www.mypage.com/blah.php Any ideas? <?php header("Content-type: image/jpg"); //include('imgsize.php'); //$insert = imagecreatefromjpeg(resizeImage('image.jpg','','650')); $insert = imagecreatefromjpeg('image.jpg'); $im = imagecreate(1800, 600); $background = imagecolorallocatealpha($im, 255, 255, 255, 127); imagecolortransparent($insert,imagecolorat($insert,50,50)); $insert_x = imagesx($insert); $insert_y = imagesy($insert); $font = 'arial.ttf'; $text = "watermark text"; $text2 = "blah blah blah \n blah blah blah"; $warpText = wordwrap($text2, 30, "\n"); $text_color = imagecolorallocate($im, 0, 0, 0); $text_color2 = imagecolorallocate($im, 0, 0, 0); imagecopymerge($im,$insert,0,0,0,0,$insert_x,$insert_y,100); imagestring($im, 8, 995, 568, $text, $text_color); imagestring($im, $font, 1280, 20, $text2, $text_color2); //imagettftext($im, 30, 0, 0, 0, $text_color2, $font, $warpText); imagejpeg($im,"",75); ?> Quote Link to comment https://forums.phpfreaks.com/topic/175813-solved-trying-to-add-white-background-and-text-to-image-with-gd/#findComment-927062 Share on other sites More sharing options...
matthewst Posted September 29, 2009 Author Share Posted September 29, 2009 Had to change $font = "arial.ttf" to $font = "./arial.ttf" to get imagettftext to work. I'm still having trouble with the image looking like a negative though. Quote Link to comment https://forums.phpfreaks.com/topic/175813-solved-trying-to-add-white-background-and-text-to-image-with-gd/#findComment-927120 Share on other sites More sharing options...
matthewst Posted September 29, 2009 Author Share Posted September 29, 2009 Strange, if I change: $insert = imagecreatefromjpeg('image.jpg'); to $insert = imagecreatefromgif('image.gif'); I get color but it looks really low quality. Quote Link to comment https://forums.phpfreaks.com/topic/175813-solved-trying-to-add-white-background-and-text-to-image-with-gd/#findComment-927174 Share on other sites More sharing options...
redarrow Posted September 29, 2009 Share Posted September 29, 2009 jpg is better then gif and png wins... Quote Link to comment https://forums.phpfreaks.com/topic/175813-solved-trying-to-add-white-background-and-text-to-image-with-gd/#findComment-927189 Share on other sites More sharing options...
matthewst Posted September 29, 2009 Author Share Posted September 29, 2009 When I change: $im = imagecreate(1800, 600); to $im = imagecreatetruecolor(1800, 600); The original image is flawless but the canvas or "background" turns black. If someone can tell me how to change that to white or transparent then I'll be all set. Quote Link to comment https://forums.phpfreaks.com/topic/175813-solved-trying-to-add-white-background-and-text-to-image-with-gd/#findComment-927223 Share on other sites More sharing options...
matthewst Posted September 29, 2009 Author Share Posted September 29, 2009 GOT IT!! $insert = imagecreatefromjpeg('image.jpg'); $im = imagecreatetruecolor(1800, 600); $white = imagecolorallocate($im, 255, 255, 255); imagefill ( $im, 0, 0, $white ); Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/175813-solved-trying-to-add-white-background-and-text-to-image-with-gd/#findComment-927232 Share on other sites More sharing options...
redarrow Posted September 29, 2009 Share Posted September 29, 2009 well done, let me see it all working friend... Quote Link to comment https://forums.phpfreaks.com/topic/175813-solved-trying-to-add-white-background-and-text-to-image-with-gd/#findComment-927239 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.