Zend pdf errors with function Image drawing - php

I want to put image to my pdf file from code function in zend that i get simple example from here http://framework.zend.com/manual/1.12/en/zend.pdf.drawing.html and this is my code
$image = Zend_Pdf_Image::imageWithPath("' . $base . '/images/logo1.png");
$page->drawImage($image, 100, 100, 400, 300);
// add page to document
$pdf->pages[] = $page;
$pdf->save("data/reports/-report.pdf");
but i get errors like this
PDF error: Cannot create image resource. File not found.
any help with it. thanks

your including single quotes inside a file name.
$image = Zend_Pdf_Image::imageWithPath("' . $base . '/images/logo1.png");
change it so it reads
$image = Zend_Pdf_Image::imageWithPath($base . "/images/logo1.png");

Related

How to get Interventation/Image after Resize in Laravel

I used Interventation image to resize a big size uploaded image. But I don't want to save it into the local directory of Laravel. This is my sample source code.
public function Uploader(Request $request) {
$upload_file = $request->file_upload;
$new_img = Image::make($upload_file)->resize(800, 544);
$new_img->save(\public_path($fileName));
$upload_file_new = "../public/" . $fileName;
}
I want to get the new path of the resized image (by not by saving it). I used
$path = $new_img->basePath();
But it returns the basePath of the $upload_file. How can I get the new path so I can use it on fopen($upload_file_new, 'r'). Please help. Thank you.
you can do like this
$image_new_name = time() . str_random(10) . str_replace(' ','',$upload_file->getClientOriginalName());

Image with laravel

Problem with laravel Image:
I have my code to store the image and it is:
$img = Image::make(asset('public/storage/assets/'.$product->image));
$img->insert(asset('template/images/logo-1000frases-w.png'), 'bottom-left', 10, 10);
$img->save(public_path('public/storage/assets/'.$product->image));
I add a watermark on the image and then I store it.
The problem is.. when I try to store the image it says:
Unable to init from given url (http://138.197.121.221/public/storage/assets/ijTdImC4dIcobYa1QSDA59oDiF8J8e0FjQS1EG3n.jpeg).
But I have the correct path... public, storage, assets, all of them exist; I wonder what could it be?
Thanks!
you may upload your imgae in laravel 5 using this way
$image=$request->file('image');
$fileName=$image->getClientOriginalName();
$path = $image->move('storage/assets/',$fileName);
$folderPath=url('/).''.'/uploads/blog/'.''.$fileName;
image is your key for request image. $folderPath variable is use for save in your database table. This may work for you
I could get a solution:
$file = $request->file('image');
$fileName = time() . '-' . $file->getClientOriginalName();
$path = $file->move('storage/assets/',$fileName);
Then:
$img = Image::make($path);
$img->insert(asset('template/images/logo-1000frases-w.png'), 'bottom-left', 10, 10);
$img->save(public_path('storage/assets/'.$product->image));
And this is it! :D

Resize copied image on upload as thumbnail

I am working on an uploader and slowly getting it working, I am uploading 3 images at once, and setting arrays for each one as keys, with an increment of ++1. I am wanting to resize the image before it gets copied to the thumbnail folder.
I have this code.
Everything works with it.
As you see, I started on getting the file info, but after that I am totally stuck on what to do after to resize the image proportionally with a maximum width of xpx and height to match it without looking distorted.
Any help would be really appreciated. Thank You.
EDIT --- I started working on it myself and wondering if this is the right approach to what i am doing.
<?php
if (isset($_POST['addpart'])) {
$image = $_FILES['images']['tmp_name'];
$name = $_POST['username'];
$i = 0;
foreach ($image as $key) {
$fileData = pathinfo(basename($_FILES["images"]["name"][$i]));
$fileName[] = $name . '_' . uniqid() . '.' . $fileData['extension'];
move_uploaded_file($key, "image/" . end($fileName));
copy("image/" . end($fileName), "image_thumbnail/" . end($fileName));
// START -- THE RESIZER THAT IS BEING WORKED ON
$source = "image_thumb/" . end($fileName);
$dest = "image_thumb/" . end($fileName);
$quality = 100;
$scale = 1 / 2;
$imsize = getimagesize($source);
$x = $scale * $imsize[0];
$y = $scale * $imsize[1];
$im = imagecreatefromjpeg($source);
$newim = imagecreatetruecolor($x, $y);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $x, $y, $imsize[0], $imsize[1]);
imagejpeg($newim, $dest, $quality);
// END -- THE RESIZER THAT IS BEING WORKED ON
$i++;
}
echo 'Uploaded<br>';
echo 'Main Image - ' . $fileName[0] . '<br>';
echo 'Extra Image 1 - ' . $fileName[1] . '<br>';
echo 'Extra Image 2 - ' . $fileName[2] . '<br>';
echo '<hr>';
}
?>
thanks
Use GD library.
Create input image object using imagecreatefromstring() for example: imagecreatefromstring(file_get_contents($_FILES['images']['tmp_name'][$i]))
It's the simplest way.
Another option is to detect file type and use functions like imagecreatefromjpeg (), imagecreatefrompng(), etc.
Create output empty image using imagecreate()
Use imagecopyresampled() or imagecopyresized() to resize image and copy+paste it from input image to output image
Save output image using function like imagejpeg()
Clean memory using imagedestroy()
The built-in image manipulation commands of PHP makes your code difficult to understand and to maintain. I suggest you to use a library which wraps it into a more productive way.
If you use Intervention/image, for example, your code will look like this:
<?php
// target file to manipulate
$filename = $_FILES['images']['tmp_name'];
// create image instance
$img = Image::make($filename);
// resize to width, height
$img->resize(320, 240);
// save it!
$img->save('thumbs/'. uniqid() . '.' . pathinfo($filename, PATHINFO_EXTENSION));
Read the full documentation here: http://image.intervention.io/use/uploads

getting image dimensions in imagick

I'm trying to get the dimensions of images that i converted with imagick. This is my code:
$imagick = new \Imagick();
$imagick->setresolution(300,300);
$imagick->readimage($this->uploadPath . '/original/test.pdf');
$imagick->writeImages($this->uploadPath . "/large/preview-%d.jpg", false);
So i'm reading a multipage pdf file and i'm converting it to seperate jpg files.
What i want is to retrieve the jpg file dimensions from each image. Can i do this in an easy way? Or do i first need to write the images to the disc and then loop through each of them again?
What about Imagick::getImageGeometry? (http://www.php.net/manual/en/imagick.getimagegeometry.php)
In your case it should look something like this:
$imagick = new \Imagick();
$imagick->setresolution(300,300);
$imagick->readimage($this->uploadPath . '/original/test.pdf');
$geometryInfo = $imagick->getImageGeometry();
// now $geometryInfo is an associative array with 'width' and 'height' items in it
$imagick->writeImages($this->uploadPath . "/large/preview-%d.jpg", false);
Update:
If you want to read concrete page from multipage PDF, try to use such trick:
$pageIndex = 0; // you can create "for/foreach" loop to go through all pages
$imagick->readimage($this->uploadPath . '/original/test.pdf[' . $pageIndex . ']');

Displaying an image created with imagecreatefromstring

Let's say I have the code that looks something like:
<?PHP
//
//... stuff here
//
$im = imagecreatefromstring( $imageData );
echo "<img src=" . /* what goes here? */ . "alt=\"the image\" />";
//
// more stuff here
//
?>
What do I replace /* what goes here? */ with so my image data will display?
Thank you.
What do I replace /* what goes here? */ with so my image data will display?
The location you highlighted is the so called src attribute of the img HTML-tagDocs. The value is a so called URIDocs.
In your case you want that URI to point to the image data in question. You have not specified which type the image should be output as, so I will assume it's a PNG image in the following example.
You now need to convert your image data into an URI. The most straight forward URI to create from the image data is a so called data: URIWikipedia:
<?PHP
//
//... stuff here
//
$im = imagecreatefromstring( $imageData );
ob_start();
imagepng($img);
$png = ob_get_clean();
$uri = "data:image/png;base64," . base64_encode($png);
echo "<img src=" . $uri /* URI goes here */ . " alt=\"the image\" />";
//
// more stuff here
//
?>
Even this is the most straight forward way, it is not always recommended to do so because the image data will be returned with the HTML to the browser. If the image is large, this is commonly considered an overhead.
Instead of using the data: URI you can place any other URI in there as well, for example a HTTP URI that is pointing to a PHP script on your server that is returning the image. Such a script can be very simple:
<?php
$img = imagecreatefromstring($string);
header('Content-type: image/png');
imagepng($img);
This is comparable to what Marc B suggested, see his answer as well.
<?php
$img = imagecreatefromstring($string);
header('Content-type: image/jpeg');
imagejpeg($img);
should be all you need. Doing it with the image tag as you are, you'd need to output the image to a temporary file and point the image tag at that (which incurs a second HTTP request), or use a data url.
I think you can do something like this...
$src = "data:image/gif;base64," . $imageData ;
echo "<img src=\"$src\" alt=\"the image\" />";
You have to save the resource to a file first or output it using something like imagepng() in a separate request.
See imagecreatefromstring() documentation for more information.
If you want to use a Data URI scheme, you can try this instead:
<?php
// If your image is binary data. use `base64_encode($imageData)`.
$imageData = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
echo '<img src="data:image/png;base64,'. $imageData .'" />';

Categories