Lassie Posted February 17, 2011 Share Posted February 17, 2011 Is it possible to both save and display a created image? I can display but not save <?php $str2=$_GET['str']; $str=$_GET['str1']; $str3="By"; $ourFileName = $_SERVER['DOCUMENT_ROOT'] . '/images/test.png'; $image = ImageCreateFromPNG("http://localhost:8888/wordpress_3/wp-content/plugins/Authors2/jackets/GDL.png"); $color = imagecolorallocate($image, 0xFF, 0xFF, 0xFF); $colorShadow = imagecolorallocate($image, 0x66, 0x66, 0x66); $font = 'Tahoma.ttf'; $fontSize = "10"; $fontRotation = "0"; /* Shadow str = title */ ImageTTFText($image, $fontSize, $fontRotation, 27, 22, $colorShadow, $font, $str ); /* Top Level */ ImageTTFText($image, $fontSize, $fontRotation, 25, 20, $color, $font, $str); /*standard narrative*/ ImageTTFText($image, $fontSize, $fontRotation, 25, 60, $color, $font, $str3); ImageTTFText($image, $fontSize, $fontRotation, 25, 100, $color, $font, $str2); header("Content-Type: image/PNG"); ImagePng ($image); imagedestroy($image); ?> Quote Link to comment Share on other sites More sharing options...
ansharma Posted February 17, 2011 Share Posted February 17, 2011 You can use second parameter to save the image like $path ='images/imagename.png'; ImagePng ($image,$path); also if you want to force download image then use headers header('Content-Disposition: Attachment;filename=imagename.png'); header('Content-type: image/png'); Quote Link to comment Share on other sites More sharing options...
Lassie Posted February 18, 2011 Author Share Posted February 18, 2011 Thank you. I can get the image with the text, but when i add the save path the image does not show, just a small question mark in a box. I cant see where this is going wrong. My code is <?php $str2=$_GET['str']; $str=$_GET['str1']; $str3="By"; $path ='http://localhost:8888/test_upload/images/imagename.png'; $image = ImageCreateFromPNG("http://localhost:8888/wordpress_3/wp-content/plugins/Authors2/jackets/GDL.png"); $color = imagecolorallocate($image, 0xFF, 0xFF, 0xFF); $colorShadow = imagecolorallocate($image, 0x66, 0x66, 0x66); $font = 'Tahoma.ttf'; $fontSize = "10"; $fontRotation = "0"; /* Shadow str = title */ ImageTTFText($image, $fontSize, $fontRotation, 27, 22, $colorShadow, $font, $str ); /* Top Level */ ImageTTFText($image, $fontSize, $fontRotation, 25, 20, $color, $font, $str); /*standard narrative*/ ImageTTFText($image, $fontSize, $fontRotation, 25, 60, $color, $font, $str3); ImageTTFText($image, $fontSize, $fontRotation, 25, 100, $color, $font, $str2); header('Content-Disposition: Attachment;filename=imagename.png'); header("Content-Type: image/PNG"); ImagePng ($image,$path); imagedestroy($image); ?> and the img tag is echo "<img src='http://localhost:8888/test_upload/image-save.php?str=$str&str1=$str1'/>"; Any advice appreciated. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted February 18, 2011 Share Posted February 18, 2011 it looks like you're attempting to save the PNG to $path AND expecting it to get sent to the browser. it's one or the other unless you add a separate line to send image to browser. http://php.net/manual/en/function.imagepng.php Quote Link to comment Share on other sites More sharing options...
ansharma Posted February 18, 2011 Share Posted February 18, 2011 if you want to display image on browser then remove this line from your code header('Content-Disposition: Attachment;filename=imagename.png'); by removing this code your image will saved at the location you specified and also displayed on browser but it will not downloaded automatically to download image you should right click on image and then coose "save image as " option Quote Link to comment Share on other sites More sharing options...
Lassie Posted February 18, 2011 Author Share Posted February 18, 2011 I tried removing the first header and I still have no image. If I remove the $path I have the image but not saved. Can I add a seperate line to save it ImagePng($image); ImagePng ($image,$path); Quote Link to comment Share on other sites More sharing options...
ansharma Posted February 18, 2011 Share Posted February 18, 2011 try this code <?php ob_start(); $str2=$_GET['str']; $str=$_GET['str1']; $str3="By"; $path ='http://localhost:8888/test_upload/images/imagename.png'; $image = ImageCreateFromPNG("http://localhost:8888/wordpress_3/wp-content/plugins/Authors2/jackets/GDL.png"); $color = imagecolorallocate($image, 0xFF, 0xFF, 0xFF); $colorShadow = imagecolorallocate($image, 0x66, 0x66, 0x66); $font = 'Tahoma.ttf'; $fontSize = "10"; $fontRotation = "0"; /* Shadow str = title */ ImageTTFText($image, $fontSize, $fontRotation, 27, 22, $colorShadow, $font, $str ); /* Top Level */ ImageTTFText($image, $fontSize, $fontRotation, 25, 20, $color, $font, $str); /*standard narrative*/ ImageTTFText($image, $fontSize, $fontRotation, 25, 60, $color, $font, $str3); ImageTTFText($image, $fontSize, $fontRotation, 25, 100, $color, $font, $str2); //header('Content-Disposition: Attachment;filename=imagename.png'); //header("Content-Type: image/PNG"); ImagePng ($image,$path); imagedestroy($image); if ($fd = fopen (Path, "r")) { header("Content-Type: image/PNG"); //header("Content-Disposition: attachment; filename=".$n); //header("Cache-control: private"); while(!feof($fd)) { $buffer = fread($fd, 2048); echo $buffer; } } fclose ($fd); ob_end_flush(); exit; ?> Quote Link to comment Share on other sites More sharing options...
Lassie Posted February 18, 2011 Author Share Posted February 18, 2011 Thanks - I tried that and i get a broken image. Quote Link to comment Share on other sites More sharing options...
ansharma Posted February 18, 2011 Share Posted February 18, 2011 Hi Lassie, if there is any error in my code then don't mind. Quote Link to comment Share on other sites More sharing options...
Lassie Posted February 19, 2011 Author Share Posted February 19, 2011 I still cant save the image. I need it to be automatically saved rather than a forced download as I need the generated image saved to the server and placed in a user refernced. For information this is being developed on a mac. Could you explain what you were intending with the code you posted to read the file please? if ($fd = fopen (Path, "r")) { header("Content-Type: image/PNG"); //header("Content-Disposition: attachment; filename=".$n); //header("Cache-control: private"); while(!feof($fd)) { $buffer = fread($fd, 2048); echo $buffer; } } fclose ($fd); ob_end_flush(); exit; ?> Quote Link to comment 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.