GD function not working - php

I'm trying to create a thumbnail images for my website. I extract the files using
$chapterZip = new ZipArchive();
if ($chapterZip->open($_FILES['chapterUpload']['tmp_name']))
{
if($chapterZip->extractTo("Manga/".$_POST['mangaName']."/".$_POST['chapterName']))
{
for($i = 0; $i < $chapterZip->numFiles; $i++) {
and then loop through the images and with the first image I send the path to a this method
function createthumb($source,$output,$new_w,$new_h)
all the values are read in fine up until I try to use the following code
if (preg_match("/jpg|jpeg/",$ext)){$src_img=imagecreatefromjpeg($source);}
if (preg_match("/png/",$ext)){$src_img=imagecreatefrompng($source);}
if (preg_match("/gif/",$ext)){$src_img=imagecreatefromgif($source);}
the prerequisite for the regular expression is being met by the file and the code is being ran, yet the imagecreate function doesn't create the new file, I checked my phpinfo file to see if the GD library is enable and it is, so in short I don't have a clue whats wrong.
http://www.neuromanga.com/phpinfo.php

make sure GD is properly installed and the function exists:
<pre>
<?
$arr = get_defined_functions();
sort($arr['internal']);
print_r($arr);
?>
also. Although this "creates" the image, you still have to write it to either the screen or a file to be able to use it. at the state it's in using imagecreatefrom[..whatever] it's just a object in your current state in your web app and has not been rendered out for storage or display. for that you need to do whatever you're going to do and use imagejpeg
or imagegif or imagepng to actually render the object back out to some destination. you can test this by executing echo $src_img which should print something like: Resource id #1

Make sure your path to $source is correct. What is $src_img if it's not a new image?

Related

PHP Image Interlacing not working with Imagick, why?

I'm trying to finish up my image uploader that utilizes imagick for the handling of various image types. One thing specifically that I'm trying to get working is the conversion of jpeg files to progressive jpeg. I've tried the following code below, but when I view the images that get output in irfranview, the jpeg are not progressive. Any ideas? This literally must work by Monday.. Please help
foreach ($thumbnailScaleWidths as $thumbnailScaleWidth) {
$thumbnail = new imagick($uploadedFile['tmp_name']);
$thumbnailDimensions = $thumbnail->getImageGeometry();
$thumbnailWidth = $thumbnailDimensions['width'];
$thumbnailHeight = $thumbnailDimensions['height'];
$thumbnailScaleHeight = ($thumbnailScaleWidth / $thumbnailWidth) * $thumbnailHeight;
$thumbnail->thumbnailImage($thumbnailScaleWidth, $thumbnailScaleHeight);
$thumbnail->setImageInterlaceScheme(Imagick::INTERLACE_PLANE);
$thumbnail->writeImages($_SERVER['DOCUMENT_ROOT'] . "/Resources/Media/$userId/$internalName-$thumbnailScaleWidth.$fileType", true);
}
Any ideas as to why this isn't outputting progressive jpegs?
I know this thread is old but here is an answer that might save time to others in the future.
So since you are reading an image from file, you should use the following method instead:
Imagick::setInterlaceScheme
It seems that Imagick::setImageInterlaceScheme will work only when Imagick is used to generate an image by itself...

Thumbnail image of PDF using ImageMagick and PHP

After searching Google and SO, I found this little bit of code for creating thumbnails of PDF documents using ImageMagick.
The trouble for me is in implementing it into my WordPress theme. I think that I'm getting stuck on the path to cache that the script needs for temporary files.
I'm using it as described in the article:
<img src="http://localhost/multi/wp-content/themes/WPalchemy-theme/thumbPdf.php?pdf=http://localhost/multi/wp-content/uploads/2012/03/sample.pdf&size=200 />
which must be right (maybe... but I assume i am correct to use full URL to the actual file), because when I click on that URL I am taken to a page that reads the following error:
Unable to read the file: tmp/http://localhost/multi/wp-content/uploads/2012/03/sample.pdf.png
Now tmp is defined in the thumbPdf.php script, but I am confused as to what it's value should be. Is it a url or a path? Like timthumb.php, can i make it be relative to the thumbPdf.php script? (I tried ./cache which is the setting in timthumb -and was sure to have a /cache folder in my theme root, to no avail). also, fyi I put a /tmp folder in my root and still get the same error.
So how do I configure tmp to make this work?
http://stormwarestudios.com/articles/leverage-php-imagemagick-create-pdf-thumbnails/
function thumbPdf($pdf, $width)
{
try
{
$tmp = 'tmp';
$format = "png";
$source = $pdf.'[0]';
$dest = "$tmp/$pdf.$format";
if (!file_exists($dest))
{
$exec = "convert -scale $width $source $dest";
exec($exec);
}
$im = new Imagick($dest);
header("Content-Type:".$im->getFormat());
echo $im;
}
catch(Exception $e)
{
echo $e->getMessage();
}
}
$file = $_GET['pdf'];
$size = $_GET['size'];
if ($file && $size)
{
thumbPdf($file, $size);
}
I have seen this answer:
How do I convert a PDF document to a preview image in PHP?
and am about to go try it next
The error tells everything you need.
Unable to read the file: tmp/http://localhost/multi/wp-content/uploads/2012/03/sample.pdf.png
Script currently tries to read file from servers tmp/ folder.
$tmp = 'tmp';
$format = "png";
$source = $pdf.'[0]';
//$dest = "$tmp/$pdf.$format";
$dest = "$pdf.$format";
Remember securitywise this doesn't really look so good, someone could exploit ImageMagic bug to achieve very nasty things by giving your script malformed external source pdf. You should at least check if the image is from allowed source like request originates from the same host.
Best way to work with ImageMagic is to always save the generated image and only generate a new image if generated image doesn't exist. Some ImageMagic operations are quite heavy on large files so you don't want to burden your server.

How to use imagick's writeImage() function?

This works if I keep the script in the same directory as the image being manipulated. And the resultant image "foo.jpg" is also generated in the same location.
<?php
$im = new imagick('image.jpg');
$im->thumbnailImage( 200, 0);
$im->writeImage("foo.jpg");
?>
But what if the script is in one location and the image I wish to work with is in another and the location I wish to save the thumbnail to is somewhere else, how to specify these paths?
Doing something like this doesn't work:
$im = new imagick('path/to/image.jpg');
Might be some file system problem. Try and get a file pointer in php first and check for any problems
$fileHandle = fopen("path/to/image.jpg", "w");
You can then use Imagick function (version 6.3.6 or newer);
$im->writeImageFile($filehandle);

create image from url using imagecreatefromjpeg

I want to create image from php using the function imagecreatefromjpeg(string $filename ),
but when I am providing a image URL as a parameter to this function then this function is not able to create image.
$pic = imagecreatefromjpeg('http://www.example.com/image.jpg');
header("Content-type: image/jpeg");
imagejpeg($pic);
imagedestroy($pic);
You can actually create images from remote files, but please be sure the 'fopen wrappers' have been enabled, see also http://php.net/manual/en/function.file.php
if it doesn't work, what kind of error do you see? and what kind of variable is $pic?
I can't check this right now, but i'd wager it has to be a local file. I.e. you need to have the file on your server.
php.net says: A URL can be used as a filename with this function if the fopen wrappers have been enabled. (http://php.net/manual/en/function.imagecreatefromjpeg.php)

Need Help Understanding this PHP Code

i am aware of the basics like what is a function, a class, a method etc. however i am totally confused on what exactly the below code does to read the image, i read it somewhere that we have to read the image in binary format first. i am confused on the process how the php reads the image and loads it for reading. i would like to know the function of each and every code in this class and what is actually happening with the code.
Code :
class Image {
function __construct($filename) {
//read the image file to a binary buffer
$fp = fopen($filename, 'rb') or die("Image $filename doesn't exist");
$buf = '';
while(!feof($fp))
$buf .= fgets($fp, 4096);
//create image
imagecreatefromstring($buf);
}
}
and when i instantiate the object image with the syntax $image = new Image("pic.jpg"); it does not print the image in html, what does the variable $image actually hold, if i want to print that image in html what should i be doing.
Update :
FYI : I understand PHP and HTML, as i was trying to learn OOP in PHP and i came across the article as this particular code was not understood clearly by me so i thought of asking you guys, i highly appreciate your response, i would be thankful if you could try and explain the code instead of asking me to try different code.
My concern is purely meant for learning purpose, i am not implementing it anywhere.
In HTML, all you need to do is refer to the file in an <img> tag.
<img src="/path/to/image/image.jpg" width="600" height="400" alt="Image Name" />
The source needs to be the URL of the image, relative to your webserver root directory.
As for the code, you put up. That would be completely unnecessary for HTML usage, and is also unnecessary for standard image use within PHP, as there are direct methods to load an image from a file, imagecreatefromjpeg() for instance for JPEG files.
As it stands, the constructor of your Image class takes a filename, opens that file and reads the entire contents as binary data in to the string $buf, 4096 bytes at a time. Then it calls imagecreatefromstring($buf) to convert the file data in to an image resource, which can then be used further within PHP with the PHP GD image handling functions.
As I say, none of this is particularly relevant if all you wish to do is display an existing image within HTML. Those commands are designed for image manipulation, inspection and creation.
Your $image would contain an instance of the Image Class.
Your constructor will try to open $filename. If that's not possible, the script will die/terminate with an error message. If $filename can be opened, the file will be read into the $buf variable and a GD image resource will be created.
The code is suboptimal for a number of reasons:
the GD resource created by imagecreatefromstring is not assigned to a property of the Image class. This means, the entire process is pointless, because the resource will be lost after it was created. The work done in the constructor is lost.
calling die will terminate the script. There is no way to get around this. It would be better to throw an Exception to let the developer decide whether s/he wants the script to terminate or catch and handle this situation.
reading a file with fopen and fread works, but file_get_contents is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.
You should not do work in the constructor. It is harmful to testability.
A better approach would be to use
class Image
{
protected $_gdResource;
public function loadFromFile($fileName)
{
$this->_gdResource = imagecreatefromstring(
file_get_contents($fileName)
);
if(FALSE === $this->_gdResource) {
throw new InvalidArgumentException(
'Could not create GD Resource from given filename. ' .
'Make sure filename is readable and contains an image type ' .
'supported by GD'
);
}
}
// other methods working with $_gdResource …
}
Then you can do
$image = new Image; // to create an empty Image instance
$image->loadFromFile('pic.jpg') // to load a GD resource
PHP's imagecreate* function return a resource. If you want to send it to the client, you'll have to set the appropriate headers and then send the raw image:
header('Content-Type: image/jpeg');
imagejpeg($img);
See the GD and Image Functions documentation for details.
class Image
{
public $source = '';
function __construct($filename)
{
$fp = fopen($filename, 'rb') or die("Image $filename doesn't exist");
$buf = '';
while(!feof($fp))
{
$buf .= fgets($fp, 4096);
}
$this->source = imagecreatefromstring($buf);
}
}
$image = new Image('image.jpg');
/* use $image->source for image processing */
header('Content-Type: image/jpeg');
imagejpeg($image->source);
If you just want to display the image, all of the above is irrelevant. You just need to write out an HTML image tag, e.g.
echo '<img src="pic.jpg" />';
That's it.
The code that you have given takes a very long and inconvenient way to load an image for manipulation using the GD library; that's almost certainly not what you wanted to do, but if you did, then you could use imagecreatefromjpeg instead.

Categories