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();
Related
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);
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?
I currently use the following code to watermark images on-the-fly and display them in a web page:
header('Content-type: image/jpeg');
$stamp = imagecreatefrompng(watermark.png');
$im = imagecreatefromjpeg($filename);
imagecopy($im, $stamp, 10, imagesy($im) - imagesy($stamp) - 10, 0, 0, imagesx($stamp), imagesy($stamp));
imagejpeg($im);
imagedestroy($im);
I also have a lot of images that are stored in zip archives. I currently display them with the following code.
$zip = new ZipArchive();
$opened = $zip->open($zipname, ZIPARCHIVE::CHECKCONS);
if ( $opened === true ){
$content = $zip->getFromName($filename);
header('Content-type: image/jpeg');
echo $content;
};
$zip->close();
I want to watermark them as well but cannot seem to get it to work. As an initial test I tried changing to output of $content from an echo to:
imagejpeg($content);
But that did not work, meaning adding the other watermark code will not work either. Any suggestions as to how to modify the zip code to include the watermark would be greatly appreciated.
I basically do not understand the difference between what is created in $im with imagecreatefromjpeg and what is extracted to $output from the zip archive. I assume that $output contains the raw jpg file data, but have no idea what $im contains.
UPDATE: Answered my own question. All I needed was to add the following after getting $content from the zip:
$im = imagecreatefromstring($content);
I can then apply the watermark exactly as in the first example. Hope that helps others.
Good day everyone.
Long story short, I'm trying to make an online card game much like urban rivals.
To do so, I'm using mainly PHP and SQL.
Right now, I'm trying to generate the png file of a card based on a database.
I've already managed to merge images with alpha channel, to add text to an image, but my real problem is : I cannot seem to be able to output this image along with other and the basic layout of a HTML document. Here is my test code :
<?php
include 'phpGDtools.php';
$image1path = 'monstercard.png';
$image2path = 'monsterimage.png';
$xdim = 220;
$ydim = 301;
//$image = mergeTwoPNGAndReturn($image1path, $image2path, $xdim, $ydim);
mergeTwoPNGAndOutputInBrowser($image1path, $image2path, $xdim, $ydim);
echo('Hello !');
?>
Sorry for the bad formatting, I haven't been able to make it better :/
Here is my function in phpGDTools.php:
function mergeTwoPNGAndOutputInBrowser($image1path, $image2path, $xdim, $ydim)
{
//TEST FUNCTION
//Both paths to the images, x and y dimensions of the image you're about to fuse.
$final_img = imagecreate($xdim, $ydim);
//Step 1 : Create the objects from the PNGs
$image_1 = imagecreatefrompng($image1path);
$image_2 = imagecreatefrompng($image2path);
//Step 1.2 : Allow transparency of bases.
imagealphablending($image_1, true);
imagesavealpha($image_1, true);
imagealphablending($image_2, true);
imagesavealpha($image_2, true);
//Step 2 : Merge the images into one.
imagecopy($image_1, $image_2, 0, 0, 0, 0, $xdim, $ydim);
imagecopy($final_img, $image_1, 0, 0, 0, 0, $xdim, $ydim);
//Step 3 : Allow transparency of final image.
imagealphablending($final_img, true);
imagesavealpha($final_img, true);
//Step 4 : Output image to browser.
header('Content-Type: image/png;');
imagepng($image_1);
}
This outputs very well the image, as intended, but the text "Hello !" is nowhere to be found.
Also, please ignore the part about "$final_image" it's just for testing.
You may have noticed I commented this line :
//$image = mergeTwoPNGAndReturn($image1path, $image2path, $xdim, $ydim);
This is the function I want to use : hopefully, my goal would be to include this php file into an html page, and then being able to print the image object between two tags.
Technically, I could save it as afile and then output the file, but that would be extremely inefficient considering my project.
Thank you for helping me out.
~Jules Courtois
You're setting the content type to an image, so anything you send back will be interpreted as an image. When you're echoing HTML you're adding to the image data which is resulting in the browser not being able to read the image.
You need to create a HTML page then reference images within the HTML file with a or tag with the src to the URL of the image or you can base64 an image to use it inline.
You could replace
imagepng($image_1);
with
ob_start();
imagejpeg($image_1);
$image_data = ob_get_contents ();
ob_end_clean ();
$image_data_base64 = base64_encode($image_data);
echo '<img src="data:image/png;base64,' . $image_data_base64 . '" />';
You should avoid processing images on the fly and try to store them but this should achieve what you asked in the question.
It looks like you are trying to output image data and html data from the same php script.
You call the function
mergeTwoPNGAndOutputInBrowser();
which outputs the image header to the browser.
And then you
echo('Hello !');
Where 'hello' is text data and not image data... So its not displayed in the image.
You could do something like this.
<?php
include 'phpGDtools.php';
$image1path = 'monstercard.png';
$image2path = 'monsterimage.png';
$xdim = 220;
$ydim = 301;
if(isset($_GET['display_image'])){
//Output image data
mergeTwoPNGAndOutputInBrowser($image1path, $image2path, $xdim, $ydim);
}
else{
//Output page data
echo 'Hello !';
echo "<img src=\"?display_image\">";
}
?>
The Text is not displaying since you have set the header
header('Content-Type: image/png;');
Why don't you use the imagestring() of the same PHP GD Library. It let's you write text on the image.
Here's a cool example. You can embed this same code to your mergeTwoPNGAndOutputInBrowser() function.
<?php
// Create a 100*30 image
$im = imagecreate(100, 30);
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write the string at the top left
imagestring($im, 5, 0, 0, 'Hello world!', $textcolor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Source
So I took the PHP code that creates this page here, and just put it inside of a new .php file, and uploaded that file here.
I just see a ton of garbage characters on the screen. Any ideas?
Here is the full PHP below:
<html>
<head>
<title></title>
</head>
<body>
<?
// gif is more appropriate than jpg for this kind of image
header("Content-type: image/gif");
// both coupon.jpg and Helvetica.ttf must be located in the same directory as
// dynamic.php or these variables must be updated.
$imgname = "./coupon.jpg";
$fontname = "./Helvetica.ttf";
// read image from disk
$im = imagecreatefromjpeg( $imgname )
or die("Source coupon image has been moved or renamed! Expected coupon.jpg");
// variable allocation
$size = 11;
$textheight = 250;
$imwidth = imagesx( $im );
$black = imagecolorallocate( $im, 0,0,0 );
// create the string containing tomorrow's date
$text = "Offer expires " .
date('l\, F j\, Y', mktime(0, 0, 0, date("m") , date("d")+1, date("Y"))).".";
// learn the width of the newly allocated string
$bbox = imagettfbbox( $size, 0, $fontname, $text );
$width = $bbox[2] - $bbox[0];
// update, display, and clear from memory the newly modified image
imagettftext($im,$size,0,$imwidth/2 - $width/2,$textheight,$black,$fontname,$text);
imagegif($im);
imagedestroy($im);
?>
</body>
</html>
Update added:
My goal is to have a Google analytics conversion script on the new page / PHP generated image, is that possible?
What did you expect? You're outputting HTML, then after some Content-type: text/plain output (i.e. the HTML), saying Content-type: image/gif
Remove the HTML around the PHP, and just keep the PHP.
Your PHP code creates an image.
This line:
header("Content-type: image/gif");
Allows to modify the HTTP response of the server to tell the browser that you are sending an image. So you do not need all of the HTML code.
You should just keep the PHP code.