i want to transfer image from localhost to a server using post. i have a problem on my php who catch the image here is my code:
$string = "iVBORw0KGgoAAAANSUhEUgAAAO0AAAA2CAIAAACk1ok0AAAAAXNSR0IArs4c6QAAAARnQU1BAACx
jwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAUJSURBVHhe7Z2hVuwwEIaRSBxIBA+ARCKRSCQS
icQhkUgkj4BEIpFIHgGJRMJ/T+6dk9uybZr8k06ys2rhJNOZf74mkzbt7nz7xxVoX4Gd9kPwCFyB
b+fYIehBAee4hyx6DM6xM9CDAs5xD1n0GJxjZ6AHBZzjNrL4/Px8enr6+PjYhrvVvVyfY+Tm8PBw
J/rgT6WEPT09hWPpHUIpgwcHB3B7d3c3wz7OgYHCsdqWv6enaU2OxwSLpnkJm87xx8cHzKoeIgOy
2S5vb2+Xl5fi9mz7cYNGIQ4hJ5JQm+PEseHq6iojYXGXz8/P29vbiRSWH6LQw9nuLy8vA/8vLi5m
e40b3N3dWR50p31LTFM9jl9fX+/v78P8OPggPV9fXxkZ+rULBrDr6+u9vb1NAqG6YB1Lz85g9kAs
Z2dnRJW4npdMFxRPFDlGJkDMzc0NFiibkDo6OirMTeIALw6ABopwqkYwmeDcDj7jzMeYhLFZ9YiF
xjvkeBFYhfKh+0TlkL5KKHeDaOHh4SGeTOzPHhiwOuR4E1j4P8YYlBYoMIhhj4u/JgawTdyjpo/n
riZmD1Rxwefj42Pi+bzIFL+uiMFCRYG6AiMKTtnYLSLHMNvNtVWMxKJMQ5OJrHlWnDr4HKecRlyO
S66tpnhbrY0AYXlJN1aDm808tXvg2IKOeeoPekkghWtfijPpRizovwLHuC7GjZxrLT1/9JaNBmLB
7docY6TBaiBEjuqZgoIFHbc5EAv61+ZY1uO43/j+/r7N6bdZaGZkZBs5lqUMLmtkSPZrFws6UmJp
NBALbtcejzVi1rBJ4XKpkRYDQaFowW3neClsiu0tALE0PGxaDG7jgvfSvsT2zjFRzFJTLXJ8cnIS
3CYWihk6OscZov3tEm8Npdx+a45juYSKVfvglm2+rFk9u+JYOFjlS+KO74k0Ncex7Kw4Pz/Pwo/W
yTmmMZ+447snjmVfHra40JDMMuQcF3FMKSckcc2Nx3Yc7orjrDPZUCc7WCSKYsdh5zgxZTWayWOw
uOtZ43jFx3COmeePHTULwZBl0+qXsVICwaNWFq4cB1eZPKUEr8Gchs2UWOhtcG8MO49DOOVXP+ju
DQxiXRtcxaMS2seate8cz0pUtYGR27wpMctWGTyoltJetY1zrCpvjnGZXpReqpTj0299TE2DzjEr
rTQ7strDF8soO8d/FKClHTX+vw/R5oqm4gd1LVfJpmRn8jSbe6UVrilBZ0VIaRCjnNJ+lTamZK/K
sdIK15SgLKSkusBeHJZNrh1TslflWGmFa0pQFivyXqzVt+BsisiU7FU5VopcySyLyDw78VPl627t
dY6HCigBp2Q2jz9iL3ntsc3VninZfTwmgjdjavYNjtg9F79a0/g9EeeYfPKYEnSC5ZL3wtc725KP
ZEp2MlLTIihFLkt7m3WkaFLyXniDLxFVymbyefRfwx44lm1iNuvI9MSEN+nv7+8LIrNfuBv5011F
S+eYfPIYryMXwYHGGeXHKjQ7x2SOrY0NS8EdtB+XH1j8haF6AvH6c9H2ciyFbGGmx92be5KCokBM
fPlTrktd2l6Og+4aisdPUrTyUNBSbqy1316O9TIRP0kR9MUUDLjpLwfp5jccynOhN7tm+MYvVTOc
oHQZozy72M9uUL8YpUjENaI3u2b42Q/HCB4oxz9hm41pSscMrb2LngJdcRzLhN8Okhffp3C5qI1G
ia+X422w/AO/8BouFT8hjwAAAABJRU5ErkJggg==";
that $string is my sample image
$img2 = base64_decode($string);
$img = imagecreatefromstring($img2);
if($img != false)
{
//this is the location and name you can use date time to create name if you want
//also put the path before the image if you want to save it to a folder ex. 'pics/image.jpg'
$path = "sample_upload/images/" . date("Y_m_d_H_i_s") . ".jpg";// put path here
$sampledate = date("Y_m_d_H_i_s");
echo $sampledate;
imagejpeg($img, $path.''.$sampledate.'.jpg');
//they can put db query here if they like to insert img path and email
}
the error is [31-Aug-2013 13:56:49 Asia/Manila] PHP Fatal error: Call to undefined function imagecreatefromstring()
the php version should be higher than 5
Related
I am trying to convert my images into greyscale and then downloading it in Laravel but each and every time I am getting this error
The file "" does not exist
Dont' know why it giving this error here is my code.
$file = public_path() . "/large/s/" . $sheet[0]->sheet_f_id . '-s.jpg';
$image = Image::make($file);
$grayScale = $image->greyscale();
return Response::download($grayScale);
When I dump my $file variable I got the response something like this.
"D:\xampp\htdocs\wikistaging\public/large/s/03-02-05-025-s.jpg"
But still it is giving me the sam error why is that happening. Any help would be great.
If you want to download then first you have to save the created file and need to give the path to download. Here is working example
$img_name=$sheet[0]->sheet_f_id . '-s.jpg';
$destination_path=public_path() . "/large/s/";
$file = $destination_path.$img_name;
$image = Image::make($file);
$image->greyscale()->save($destination_path.'gray-'.$img_name);
return Response::download($destination_path.'gray_'.$img_name);
And if you don't want to keep the file you can delete, replace the last line with below line.
return Response::download($destination_path.'gray_'.$img_name)->deleteFileAfterSend(true);
Hope it will work for you.
You must save your image after call greyscale().
You can try it:
$filePath = public_path() . "/large/s/" . $sheet[0]->sheet_f_id . '-s-test.jpg';
$image->greyscale();
$image->save($filePath);
I want download an image from AWS S3 and process it with php. I am using "imagecreatefromjpeg" and "getimagesize" to process my image but it seem that
Storage::disk('s3')->get(imageUrlonS3);
retrieve the image in binary and is giving me errors. This is my code:
function createSlices($imagePath) {
//create transform driver object
$im = imagecreatefromjpeg($imagePath);
$sizeArray = getimagesize($imagePath);
//Set the Image dimensions
$imageWidth = $sizeArray[0];
$imageHeight = $sizeArray[1];
//See how many zoom levels are required for the width and height
$widthLog = ceil(log($imageWidth/256,2));
$heightLog = ceil(log($imageHeight/256,2));
//more code here to slice the image
.
.
.
.
}
// ex: https://s3-us-west-2.amazonaws.com/bucketname/image.jpg
$content = Storage::disk('s3')->get(imageUrlonS3);
createSlices($content);
What am I missing here ?
Thanks
I think you are right in your question what the problem is - the get method returns the source of the image of itself, not the location of the image. When you pass that to createSlices, you're passing the binary data, not its file path. Inside of createSlices you call imagecreatefromjpeg, which expects a file path, not the image itself.
If this indeed the case, you should be able to use createimagefromstring instead of createimagefromjpeg and getimagesizefromstring instead of getimagesize. The functions createimagefromstring and getimagesizefromstring each expects the binary string of the image, which I believe is what you have.
Here's the relevant documentation:
createimagefromstring - http://php.net/manual/en/function.imagecreatefromstring.php
getimagesizefromstring - http://php.net/manual/en/function.getimagesizefromstring.php
Resulting code might look something like this:
function createSlices($imageData) {
$im = imagecreatefromstring($imageData);
$sizeArray = getimagesizefromstring($imageData);
//Everything else can probably be the same
.
.
.
.
}
$contents = Storage::disk('s3')->get($imageUrlOnS3);
createSlices($contents);
Please note I haven't tested this, but I believe from what I can see in your question and what I read in the documentation that this might just do it.
I have an image that is sent from an iPad app to an SQL database. I can retrieve this image and display in a web page using the following php:
$img = base64_encode($row['photoData']);
echo "<img src=\"data:image/jpg;charset=utf8;base64, $img\"/>";
This displays fine. What I want to do now is put this image into a PDF document using FPDF however I am struggling to do this.
This:
$img = base64_encode($row['photoData']);
$pdf->Image($img);
give this error:
FPDF error: Image file has no extension and no type was specified:
So I tried this (although I realise I will then have to look at how to get the size of the image sorted):
$pdf->Image($img, 20, 20, 20, 20 'JPG');
which give me:
FPDF error: Missing or incorrect image file:
What is the correct way to do this?
Or would it be easier to temporarily save the image to the server and then place the saved image into the PDFdoc?
As mentioned in the comments above this is possible by using a stream ("data url") to hand over the image data to the fpdf library without writing physical files to disk:
<?php
// load the 'fpdf' extension
require('fpdf.php');
// just for demonstration purpose, the OP gets the content from a database instead
$h_img = fopen('img.jpg', "rb");
$img = fread($h_img, filesize('img.jpg'));
fclose($h_img);
// prepare a base64 encoded "data url"
$pic = 'data://text/plain;base64,' . base64_encode($img);
// extract dimensions from image
$info = getimagesize($pic);
// create a simple pdf document to prove this is very well possible:
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello Image!');
$pdf->Image($pic, 10, 30, $info[0], $info[1], 'jpg');
$pdf->Output();
If this is a good advice is another question, this is merely meant to prove that this is possible...
According to the Docs FPDF::Image accepts a filename as the first argument, not a binary blob.
If you want to use FPDF specifically, save the image to a temporary file first, and then pass that to FPDF::Image.
To do that, something like this should work:
$tmpFile = tempnam(sys_get_temp_dir(), 'fpdfimg');
if (file_put_contents($tmpFile, $row['photoData'])) {
$fpdf->Image($tmpFile);
// save/display image
unlink($tmpFile);
}
Alternatively, if you want to just serve the image as a PDF (with no other content) you could use Imagick:
$im = new \Imagick();
$im->readImageBlob($row['photoData']);
$im->setImageFormat('pdf');
header('Content-Type: application/pdf');
echo $im;
Since FPDF cannot use base64 data to produce images on the PDF, I would recommend saving the file to the disk permanently as opposed to writing a temp file for every PDF operation.
This will save you a lot of I/O overhead.
Assuming your table has unique photo_id or photo_name to accompany photoData then you can use something like this to create your images and use them in FPDF.
I will also assume you have a last_update and photo_extension column.
<?php
$path = '/path/to/fpdf/images/';
$filename = $row['photo_id'].'.'.$row['photo_extension'];
$filepath = $path.$filename;
// If a physical file is not available then create it
// If the DB data is fresher than the file then make a new file
if(!is_file($filepath) || strtotime($row['last_update']) > filemtime($filepath))
{
$result = file_put_contents($filepath, $row['photoData']);
if($result === FALSE)
{
die(__FILE__.'<br>Error - Line #'.__LINE__.': Could not create '.$filepath);
}
}
$pdf->Image($filepath);
If you plan on updating the photoData which is stored in your DB then you will have to make sure to also have a timestamp column and compare that timestamp against the filemtime($filepath) of the image on your disk.
Another solution for this ;)
Make a new php by copying and pasting this (piece of fpdf's code edited):
require('fpdf.php');
class DATAIMAGE extends FPDF
{
protected function _parsedata($file)
{
// Extract info from a JPEG file
$a = getimagesizefromstring($file);
if(!$a)
$this->Error('Missing or incorrect image file: '.$file);
if($a[2]!=2)
$this->Error('Not a JPEG file: '.$file);
if(!isset($a['channels']) || $a['channels']==3)
$colspace = 'DeviceRGB';
elseif($a['channels']==4)
$colspace = 'DeviceCMYK';
else
$colspace = 'DeviceGray';
$bpc = isset($a['bits']) ? $a['bits'] : 8;
return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$file);
}
}
Then call this php instead of fpdf.php in your main php.
You'll now be able to display an image simply by adding 'data' to the end of the function:
$pdf->Image($mysqlrow["blob"],0,0,40,0,'data');
I have a data uri variable in php
$imageURL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyAAAAJYCAYAAACadoJwAAAgAElEQ…ACIiACIiAC5U1AAqS891erEwEREAEREAEREAEREIFAEfj/bfXX..."
I am trying to insert this into a pdf using fpdf for which I need to convert this into a image I guess. I tried doing something like
base64_decode($imageURL);
but this does not work. How I successfully insert this data uri into pdf.
$image_content = base64_decode(str_replace("data:image/png;base64,","",$imageURL)); // remove "data:image/png;base64,"
$tempfile = tmpfile(); // create temporary file
fwrite($tempfile, $image_content); // fill data to temporary file
$metaDatas = stream_get_meta_data($tempfile);
$tmpFilename = $metaDatas['uri'];
Now you can use that image into fpdf like:
$pdf->Image($tmpFilename,null,null,0,0);
Or you can specify image type by adding image type parameter like this:
$pdf->Image($tmpFilename,null,null,0,0,'PNG');
Please check to http://www.fpdf.org/en/doc/image.htm
I am using getimagesize() to get teh size of my image. I have image in my sub-directory when i am passing file name with sub-directory i ma facing an error:
There is no such file or directory
here is my Code:
<?PHP
function resize($img,$max_w){
$max_h=$max_w;
list($img_w,$img_h) = getimagesize($img);
$f = min($max_w/$img_w, $max_h/$img_h, 1);
$w = round($f * $img_w);
$h = round($f * $img_h);
return array($w,$h);
}
$filename="user_img/"."1256115556.jpg";
$resize=resize($filename,667);
$w=$resize[0];
$h=$resize[1];
?>
instead of this when i passing
$filename="1256115556.jpg";
file from my main directory the function is running perfectly. So please help me, How to pass file with sub-directory.
Pass your directory name inside getimagesize(). Because the file name is a String.
list($img_w,$img_h) = getimagesize("user_img/".$img);
Its working fine now.
If you're on a windows box then you might try a "\" and not a "/" in your path.