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);
Related
I am trying to migrate some content from one resources into another and need to save some images (several hundred) located at a remote resource.
Suppose I have only the URL to an image:
https://www.example.com/some_image.jpg
And I would like to save it into the filesystem using PHP.
If I were uploading the image, I essentially would do the following:
<input type="file" name="my_image" />
move_uploaded_file($_FILES['my_image']['tmp_name'], '/my_img_directory');
But since I only have the URL, I would imagine something like:
$img = 'https://www.example.com/some_image.jpg';
$file = readfile($img);
move_uploaded_file($file, '/my_img_directory');
Which of course wouldnt work since move_uploaded_file() doesn't take an output buffer as a first argument.
Essentially, I would need to get $img into the $_FILES[] array under this approach. Or may some other approach?
You can use PHP's copy function to copy remote files to a location on your server:
copy("https://example.com/some_image.jpg", "/path/to/file.jpg");
http://php.net/manual/en/function.copy.php
$image = file_get_contents('http://www.url.com/image.jpg');
file_put_contents('/images/image.jpg', $image); //Where to save the image on your server
i m trying to resize a image using code:
list($width,$height,$type,$attr)= getimagesize($_FILES['upload'.$num]['name']);
$source = imagecreatefrompng($_FILES['upload'.$num]['name']);
$thumb = imagecreatetruecolor(445,320);
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
imagecopyresampled($thumb,$source,0,0,0,0,445,320,$width,$height);
imagepng($thumb,"../public/img/".$Nome,8);
but the output is always a black image.. anyone know why?
Thanks
$_FILES['upload'.$num]['name'] is just filename of uploaded like "flower.jpg" not full path to file.
$_FILES['upload'.$num]['tmp_name'] is real absolute path to real file uploaded on your server (somewhere in temp directory)
Your code should look like this:
list($width,$height,$type,$attr)= getimagesize($_FILES['upload'.$num]['tmp_name']);
$source = imagecreatefrompng($_FILES['upload'.$num]['tmp_name']);
Always try to debug your first. Use functions like print_r($_FILES), var_dump($_FILES) to debug your variables.
except issue with $_FILES variable, your code should works fine: Demo
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.
I have moved my images to Rackspace Cloud Files and am using their PHP API. I am trying to do the following:
Get an image from my "originals" container
Resize it, sharpen it, etc.
Save the resized image to the "thumbs" container
My problem is with #2. I was hoping to resize without having to copy the original to my server first (since the images are large and I'd like to resize dynamically), but can't figure out how. This is what I have so far (not much):
$container = $conn->get_container("originals");
$obj = $container->get_object("example.jpg");
$img = $obj->read();
Part of the problem is I don't fully understand what is being returned by the read() function. I know $img contains the object's "data" (which I was able to print out as gibberish), but it is neither a file nor a url nor an image resource, so I don't know how to deal with it. Is it possible to convert $img into an image resource somehow? I tried imagecreatefromjpeg($img) but that didn't work.
Thanks!
First, you cannot resize an image without loading it into memory. Unless the remote server offers some "resize my image for me, here are the parameters" API, you have to load the image in your script to manipulate it. So you'll have to copy the file from the CloudFiles container to your server, manipulate it, then send it back into storage.
The data you receive from $obj->read() is the image data. That is the file. It doesn't have a file name and it's not saved on the hard disk, but it is the entire file. To load this into gd to manipulate it, you can use imagecreatefromstring. That's analogous to using, for example, imagecreatefrompng, only that imagecreatefrompng wants to read a file from the file system by itself, while imagecreatefromstring just accepts the data that you have already loaded into memory.
You can try to dump the content of the $img variable into a writable file as per the below:
<?php
$filename = 'modifiedImage.jpg';
/*
* 'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate
* the file to zero length. If the file does not exist, attempt to create it.
*/
$handle = fopen($filename, 'w+');
// Write $img to the opened\created file.
if (fwrite($handle, $img) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote to file ($filename)";
fclose($handle);
?>
More details:
http://www.php.net/manual/en/function.fopen.php
http://www.php.net/manual/en/function.fwrite.php
Edit:
You might also want to double check the type of data returned by the read() function, because if the data is not a jpg image, if it's for example a png, the extension of the file needs to be changed accordingly.
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?