I'm trying to display a png image in PHP (Laravel)
So far I've tried
$response = Response::make(readfile(public_path() .
"/img/$image_name.png", 200))->header('Content-Type', 'image/png');
return $response;
and this
$file = public_path() . "/img/$image_name.png";
$im = imagecreatefrompng($file);
header('Content-Type: image/png');
imagePNG($im);
imagedestroy($im);
I always get something like this instead of an actual image
�PNG IHDR:�c PLTE����>tRNS�* �< pHYs���+uIDAT(�eұ� AJs *́[P��#8Ҍ��Y�:����s�A�"D�!B�"D�!B�"D�!C~����}��Q��N�+'��bP�.a&^O)%5Y\�L����.ޜ9��IEND�B`�
When I use jpeg header and jpeg image it starts showing the actual image in browser, but for some reason it doesn't work for png images. Have anyone faced a similar problem?
Try it! Keep it simple :)
$img = file_get_contents(public_path('yourimage.jpg'));
return response($img)->header('Content-type','image/png');
You should find the mime dynamicaly using laravel default file facade File::mimeType($path) because as you mentioned that it is working fine with another image type like jpeg. I think this may happen because sometime we change file extension for testing or may be file have some issue with its header etc. You can use the following code :
Method 1 use Response::stream:
$path = public_path() . "/img/$image_name.png";
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::stream(function() use($file) {
echo $file;
}, 200, ["Content-Type"=> $type]);
Method 2 use Response::make:
$path = public_path() . "/img/$image_name.png";
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
Hope it will help.
perhaps you forgot install php-gd extension on your server
sudo apt-get install php-gd
or
sudo yum install php-gd
I suggest you use this plugin: http://image.intervention.io/
Related
I'm trying to change all images uploaded to png format. I'm using the Intervention Image package for Laravel, and calling the encode function. Image Files are not changing to .png
Here is my upload script: (Everything is uploading, resizing and appears to be compressing. Just not converting to a png file)
if($request->hasFile('listing_image')){
$classifiedImg = $request->file('listing_image');
$filename = 'listing'.'-'.uniqid().'.'.$classifiedImg->getClientOriginalExtension();
Image::make($classifiedImg)->encode('png', 65)->resize(760, null, function ($c) {
$c->aspectRatio();
$c->upsize();
})->save(public_path('/images/users/listing-images/' . $filename));
$classified->listing_image = $filename;
$classified->save();
}else{
$classified->save();
}
Am I doing something wrong in this section:
Image::make($classifiedImg)->encode('png', 65)->resize(760, null, function ($c)...
OR is this causing the issue:
getClientOriginalExtension();
Thanks BagusTesa, you were correct. This was causing the issue.
getClientOriginalExtension();
To get the extension to convert. I needed to add the extension to the file name.
Change this line:
$filename = 'listing'.'-'.uniqid().'.'.$classifiedImg->getClientOriginalExtension()
To This:
$filename = 'listing' . '-' . uniqid() . '.png';
I'm using Imagick and trying to convert a pdf to a png. It fails. My error_log says "Failed to read the file".
Example code:
$fileone = $_SERVER['DOCUMENT_ROOT'] . '/' . 'test.pdf';
$image = new Imagick($fileone);
$image->readImage($fileone);
$image->thumbnailImage(300, 0);
echo '<img src="data:image/png;base64,' . base64_encode($image->getimageblob()) . '" />';
Thoughts?
You need to install ghostscript
sudo apt-get install ghostscript
I would first use realpath() to check your file path and then see if the file is readable.
$fileone = realpath('test.pdf');
if (!is_readable($fileone)) {
echo 'file not readable';
}
Then if it is a multiple page pdf try this
$image = new Imagick($fileone.'[0]');
When I try to upload an image file to the server, randomly it gives error and sometimes it works fine with no problems with the same file.
Symfony \ Component \ HttpFoundation \ File \ Exception \ FileException
The file "xxxxxx.jpg" was not uploaded due to an unknown error.
This is my code:
if (Input::hasFile('thumbnail_image')){
// Upload Photo
$thumbnailImage = Input::file('thumbnail_image');
$filename = 'thumbnail.'.Input::file('thumbnail_image')->getClientOriginalExtension();
$destinationPath = 'training_product/article_id_'. $id .'/thumbnail_image';
$databasePath = '/adidas/public/training_product/article_id_'. $id.'/thumbnail_image';
$uploadFile = Input::file('thumbnail_image')->move($destinationPath, $filename);
$product->thumbnail_images_path = $databasePath.'/'.$filename;
$product->save();
}
What are your file permission on your destination folder? Try changing it to 777(assuming that you are using a linux server: sudo chmod -R 777 adidas/public/training_product.
I've modified your code slightly and it works for me.
```
if (Input::hasFile('thumbnail_image')){
// Upload Photo
$product = new Product;
$thumbnailImage = Input::file('thumbnail_image');
$ext = $thumbnailImage->guessExtension();
$filename = 'thumbnail_'. time() . $ext;
$destinationPath = 'training_product/article_id_'. $id .'/thumbnail_image';
$databasePath = '/adidas/public/training_product/article_id_'. $id.'/thumbnail_image/';
$thumbnailImage->move($destinationPath, $filename);
$product->thumbnail_images_path = $databasePath . $filename;
$product->save();
}
```
I've Changed getClientOriginalExtension() to guessExtension(), instantiated a new $product, as I didn't see you doing. Also made sure the folder permissions would allow me to upload files.
Hope it helps.
if (file_exists($path)) {
$fileContent = id3_getImage($path);
$fileMime = id3_getMimeOfImage($path); // is set to image/jpeg
if ($fileMime != "") {
header('Content-Type: '.$fileMime);
print($fileContent);
die;
}
}
So the above code does not work in the browser, however when I make a image with image
$img = imagecreatefromstring($fileContent);
imagejpeg($img, 'test.jpg');die;
the image is created and I can view it on my computer. So I have no clue what I am doing wrong :(
*Could it be a setting in the apache conf? I honestly have no clue on this one
header('Content-Type: image/jpeg');
// Output the image
imagejpeg($img);
You need send header before. But if you want to create the image and show, you need create, and read this for the browser.
readfile($filename);
You can read the man here:
http://php.net/manual/en/function.readfile.php
I use smartReadFile.php from Google Groups to display any type of file.
https://jplayer.googlegroups.com/attach/f308294ddea52f6c/smartReadFile.php?view=1&part=4
It's really smart. =)
<?php
require "smartReadFile.php";
$dir = 'images/';
$filename = 'someimage' . '.jpg';
$location = $dir.$filename;
smartReadFile($location, $filename);
?>
This one is working for me when reading from DB
$image = imagecreatefromstring([path/DBfiels]);
header('content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
My application is having images sent to it from mobile devices with the content-type "application/octet-stream".
I need to process these images using the GD library, which means I need to be able to create an image object from the data.
Typically, I have been using imagecreatefromjpeg, imagecreatefrompng, imagecreatefromgif, etc, to handle files uploaded from web forms, but these don't seem to work when coming to me as application/octet-stream.
Any ideas on how I can achieve my goal?
EDIT
Here is the code I use to create the image identifier...my handler works perfectly throughout my site, the only difference I can tell between my site and the data from the iOS is the content-type
public function open_image($path) {
# JPEG:
$im = #imagecreatefromjpeg($path);
if ($im !== false) { $this->image = $im; return $im; }
# GIF:
$im = #imagecreatefromgif($path);
if ($im !== false) { $this->image = $im; return $im; }
# PNG:
$im = #imagecreatefrompng($path);
if ($im !== false) { $this->image = $im; return $im; }
$this->error_messages[] = "Please make sure the image is a jpeg, a png, or a gif.";
return false;
}
Easy :)
$image = imagecreatefromstring( $data );
Specifically:
$data = file_get_contents($_FILES['myphoto']['tmp_name']);
$image = imagecreatefromstring( $data );
I found this in the codeigniter forum a way to change the mime and it works, I suppose you can use for other frameworks as well, this is the link and this the code:
//if mime type is application/octet-stream (psp gives jpegs that type) try to find a more specific mime type
$mimetype = strtolower(preg_replace("/^(.+?);.*$/", "\\1", $_FILES['form_field'] ['type'])); //reg exp copied from CIs Upload.php
if($mimetype == 'application/octet-stream'){
$finfo = finfo_open(FILEINFO_MIME, '/usr/share/file/magic');
if($finfo){
$_FILES['form_field']['type'] = finfo_file($finfo, $_FILES['form_field']['tmp_name']);
finfo_close($finfo);
}
else echo "finfo_open() returned false");
}
Fileinfo extension needs to be installed on the server.
It worked for me.
you can use this function too. it not require any other dependency. and work perfectly for other types also.
function _getmime($file){
if($info = #getimagesize($file)) {
return image_type_to_mime_type($info[2]);
} else {
return mime_content_type($file);
}
}