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);
Related
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
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?
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get image resource size in bytes with PHP and GD?
Is it possible to get the file size (not the image size dimensions) of object $image using php? I would like to add this to my "Content-Length:" header.
$image = imagecreatefromjpeg($reqFilename);
I think this should work:
$img = imagecreatefromjpeg($reqFilename);
// capture output
ob_start();
// send image to the output buffer
imagejpeg($img);
// get the size of the o.b. and set your header
$size = ob_get_length();
header("Content-Length: " . $size);
// send it to the screen
ob_end_flush();
You could just use filesize() for this:
// returns the size in bytes of the file
$size = filesize($reqFilename);
The above will of course only work if the resized image is the one stored on disk if you will resize the image after your call to imagecreatefromjpeg() then you should go with #One Trick Ponys solution and do something like this:
// load original image
$image = imagecreatefromjpeg($filename);
// resize image
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// get size of resized image
ob_start();
// put output for image in buffer
imagejpeg($new_image);
// get size of output
$size = ob_get_length();
// set correct header
header("Content-Length: " . $size);
// flush the buffer, actually send the output to the browser
ob_end_flush();
// destroy resources
imagedestroy($new_image);
imagedestroy($image);
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();
I am using PHP imagejpeg to create a simple image like this..
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');
// Output the image
imagejpeg($im);
// Free up memory
imagedestroy($im);
This is working, but i would like it to automatically trigger a download inside the browser instead of display the image.
How can I do this, do I need to set a header?
All you have to do is set an extra header:
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename=file-name.jpg'); // This will tell the browser to download it
// Output the image
imagejpeg($im);
// Free up memory
imagedestroy($im);
Here is the documentation
I believe you need at least 3 headers:
header("Content-Type: image/jpeg"); // Content type
header("Content-Disposition: attachment; filename=file-name.jpg"); // Content deposition
header("Content-Length: $fileSize"); // YOUR FILE SIZE
otherwise, the download will not work in firefox.