Save image on php imagepng - php

I have in a single PHP file an Upload and then, I use imagecreatefrompng to link 2 images, the uploaded image, and another I have in my website.
It works fine in another php file, but when I try to use it in the upload php file, it doesn't save the imagepng($src, "name.png");
The code is:
imagecopymerge($src, $dest, 0, 0, 0, 0, 600, 600, 45);
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
$cad = "";
for($i=0;$i<12;$i++) {
$cad .= substr($str,rand(0,62),1);
}
imagepng($src, "fp/$cad.png");
header('Content-Disposition: Attachment; filename="flagperfil.png"');
header('Content-Type: image/png');
imagedestroy($dest);
imagedestroy($src);
I can't put header('Content... because is a PHP file who has a lot of information. If i put this, then it won't be displayed.
Thanks

Related

Telegram bot API error 400 Photo has unsupported extension

I am trying to send a dynamically created png image (php script that generates a png file, [code below]) through one of my telegram bots using "sendPhoto" method.
It works fine when I link a physical png file (in the multipart field parameter), but when I try and use the php file, I receive the following error from Telegram:
Photo has unsupported extension. Use one of .jpg, .jpeg, .gif, .png, .tif or .bmp
The PHP code is simple enough, works well when I open the file in my browser (i'm shown a dialog to download the png file and it opens fine on my computer):
header("Content-type: image/png");
header('Content-Disposition: attachment; filename="moo.png"');
$image = imagecreatetruecolor(50, 50);
imagesavealpha($image, true);
$transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparent);
ob_start ();
imagepng($image);
ob_end_flush();
imagedestroy($image);
Is there a way I can bypass this extension check and send my dynamic image script (file with .php extension) in my telegram request?
turns out bot API had nothing to do with it.
i wrongfully attached the script file when I really should have needed to attach the script output (duh!) :/
So, flushing the output buffer into a tmpfile and then sending it did the trick
function pngCraft($width = 100, $height = 100, $asBase64 = FALSE)
{
$image = imagecreatetruecolor($width, $height);
imagesavealpha($image, true);
$transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparent);
ob_start ();
imagepng($image);
$image_data = ob_get_contents();
ob_end_clean ();
$out = ($asBase64) ? base64_encode($image_data) : $image_data;
imagedestroy($image);
return $out;
}
$file = tmpfile();
fwrite($file, pngCraft());
fseek($file,0);
$a = stream_get_meta_data($file);
$uri = $a['uri'];
rename($uri, $uri.='.png');
$fields[TG_METHOD_SENDPHOTO_PHOTO] = '#'.$uri;
$fields[TG_METHOD_SENDPHOTO_CAPTION] = 'Yay!';

How to save image created by PHP imagecopymerge function

code:
$dest = #imagecreatefrompng(base_url().'assets/images/card-design-3-backside.png');
$src = #imagecreatefrompng(base_url().'assets/images/card-design-3-ackside.png);
$sj = imagecopymerge($dest, $src, 845, 280, 0, 0, 200, 200, 100);
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
If you want to save image to a file, just adding the 2nd parameter to the imagepng() function. Like this:
imagepng($dest, "file.png");
Don't need add a header because we don't want to output to the browser.
Presumably you mean save to disk or download. From the manual:
SAVE TO DISK (On server):
<?php
// Create a destination
$dest = 'new/spot/new_name.png';
// Get contents from original spot
$im = imagecreatefrompng("https://www.fagario.com/skins/by-hacker.png");
// Save to disk in the new spot and name
imagepng($im,$dest);
// Destroy image
imagedestroy($im);
DOWNLOAD (to local):
<?php
$dest = "name.png";
// Get contents from original spot
$im = imagecreatefrompng("https://www.fagario.com/skins/by-hacker.png");
header('Content-Disposition: Attachment; filename='.$dest);
// Create the proper file type header
header('Content-Type: image/png');
imagepng($im);
// Destroy image
imagedestroy($im);
PRESENT IN BROWSER (No download or saving):
<?php
// Get contents from original spot
$im = imagecreatefrompng("https://www.fagario.com/skins/by-hacker.png");
// Create the proper file type header
header('Content-Type: image/png');
imagepng($im);
// Destroy image
imagedestroy($im);

php imagegif() direct output fails but indirect output works

I have a php script that is supposed to create and display an image.
The image is displayed properly on the webpage when using this code:
header('Content-Type: image/gif');
readfile('picture.gif');
//rest of code creating the image
imagegif($im,'picture.gif');
But, the image is not displayed when using this code:
header('Content-Type: image/gif');
//rest of code creating the image
imagegif($im);
In the php documentation for imagegif, this code is given as an example:
<?php
// Create a new image instance
$im = imagecreatetruecolor(100, 100);
// Make the background white
imagefilledrectangle($im, 0, 0, 99, 99, 0xFFFFFF);
// Draw a text string on the image
imagestring($im, 3, 40, 20, 'GD Library', 0xFFBA00);
// Output the image to browser
header('Content-Type: image/gif');
imagegif($im);
imagedestroy($im);
?>
What am I doing wrong?

PHP Overlay GIF on JPEG

At the moment I am generating a barcode using Shay Anderson's class (http://www.shayanderson.com/php/php-barcode-generator-class-code-39.htm) and I am able to successfully display the generated barcode in the browser as follows:
$bc = new PrintBarcode('DARP CODE');
$bc->drawVoucher();
$src = $bc->getVoucher();
// Output and free from memory
header('Content-Type: image/jpeg');
imagejpeg($src);
but now I am trying to modify my script to overlay the barcode on top of another image to create a voucher but I can't seem to get it to work, I just get the broken image icon in Chrome and the following warning in the console:
Resource interpreted as Document but transferred with MIME type image/jpeg
$bc = new PrintBarcode('DARP CODE');
$bc->drawVoucher();
$src = $bc->getVoucher();
header('Content-Type: image/jpeg');
// create actual voucher with barcode overlayed on voucher background
$bg = imagecreatefromjpeg('images/voucher.jpg');
imagecopymerge($bg, $src, 0, 0, 0, 0, imagesx($bg), imagesy($bg), 75);
imagejpeg($bg, null, 100);
imagedestroy($bg);
Error reporting is on and I'm getting no wanrings, notices or fatal errors. Any help appreciated.
The only thing I can think of is that from the docs of the barcode class, it generates the barcode as a gif so not sure if I am missing a few steps.
Turns out the problem was to do with the image I was using as the basis of the merge wasn't quite right so I re-converted it from a png to jpg properly (first time I downloaded the png I simply did a save as all files to jpeg) using photoshop and it was fine, to clarify, here's the code:
$bc = new PrintBarcode('DARP CODE');
$bc->drawVoucher();
$src = $bc->getVoucher();
header('Content-Type: image/jpeg');
$bg = imagecreatefromjpeg('images/voucher.jpg');
imagecopymerge($bg, $src, 40, 380, 0, 0, imagesx($bg), imagesy($bg), 100);
imagejpeg($bg, null, 100);
imagedestroy($src);
imagedestroy($bg);

Cannot create handle for image after obtaining source code

I am trying to create a small experimental script for obtaining comic strips from web-comic sites using php. The site that I am playing with is Fredo and Pidjin. Here is the code that I have written so far:
<?php
require_once "../shdp/simple_html_dom.php";
$next = "http://www.pidjin.net/2012/08/28/of-my-own/";
$html = file_get_html($next);
$imageList = $html->find('div[class=episode] p img');
for($iii=0; $iii<count($imageList); $iii++){
$storage[$iii] = $imageList[$iii]->src;
}
$img = file_get_contents($storage[0]);
$img = imagecreatefromstring($img);
header("Content-type: image/png");
$something = imagepng($img);
?>
For scraping the html, I am using the Simple HTML DOM parser.
This is what I'm trying to do: Get the src of the image and assign it to a handle. Subsequently find it for all the comic panels on the page and use imagecopy to make one strip that can be saved to the computer. The code, however, is in the preliminary stages and I have not got to the imagecopy part yet.
Problem: While imagepng(...) outputs the file on the browser, I am not able to get a $src handle (so to speak) on the image to use in imagecopy.
Thanks in advance.
How are you trying to get a handle to the image?
$src = imagecreatefrompng($storage[0]);
$dest = imagecreatetruecolor(80, 40);
imagecopy($dest, $src, 0, 0, 20, 13, 80, 40);
header('Content-Type: image/png');
imagepng($dest);
In case you need to show multiple images try with output buffering:
ob_start();
header('Content-Type: image/png');
imagepng($img1);
header('Content-Type: image/png');
imagepng($img2);
ob_end_flush();

Categories