zend saving image from url - php

i am extracting all img urls from an array and getting image urls from external sites...
how do i go about saving these images?
i tried using
$upload = new Zend_File_Transfer_Adapter_Http();
$upload->setDestination("img/");
# Returns the file name for 'doc_path' named file element
$name = $upload->getFileName('http://zendguru.files.wordpress.com/2009/04/ajax-form1.jpg');
but im getting nothing saved plus its saying
Call to undefined method:- Zend_File_Transfer_Adapter_Http::setOption()

I think that you cannot do it using Zend_File_Transfer_Adapter_Http as this works only with uploaded files. However, you could use Zend_Http_Client for this. For example:
$c = new Zend_Http_Client();
$c->setUri('http://zendguru.files.wordpress.com/2009/04/ajax-form1.jpg');
$result = $c->request('GET');
$img = imagecreatefromstring($result->getBody());
imagejpeg($img,'img/test.jpg');
imagedestroy($img);

You should not use Zend for this. Plain PHP would do.
$ch = curl_init('http://zendguru.files.wordpress.com/2009/04/ajax-form1.jpg');
$fp = fopen('/img/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Related

How to post a PNG image data(fetched from website A) to form-data style?

I've got a website A that holds an image, and I am now using PHP to grab that image and I want to post it via form-data media to another website.
My solution now is to save the image as a file and then use CURLFile to auto-generating these form-media post fields.
Is there a way that I don't need to save this image to my local file system?
What I am doing now:
$img = urlcurl::geturl($this->url); // seems to be an raw image data (png?)
$fp = fopen('./tmp/1.png','x');
fwrite($fp, $img);
$t = new CURLFile("/tmp/1.png","image/png"); // get from local file storage
$filedata = array("media" => $t);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_POSTFIELDS, $filedata);
$response = curl_exec($ch);

Highchart save chart image using cURL

I am using HighCharts and saving image of chart using cURL. It is working fine at my localhost. But when I try the same code on server, the image is blank. And in server error_log I found this warning message:
PHP Warning: imagecreatefromjpeg(): '10361254147.jpeg' is not a valid JPEG file in public_html/project/assign_img.php on line 34
The code that I am using is as below:
$imgNm = 'https://export.highcharts.com/charts/chart.2ce468213abe432aa1c288339f90171e.jpeg';
$img = 'xyz.jpeg';
$ch = curl_init($imgNm);
$fp = fopen($img, "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
imagecreatefromjpeg($img);
Not sure where I am wrong.
If you just want to save your file and show in your browser you don't need cURL:
$imgNm = 'https://export.highcharts.com/charts/chart.2ce468213abe432aa1c288339f90171e.jpeg';
$filename = 'xyz.jpeg';
$image_data = file_get_contents($imgNm);
file_put_contents($filename, __DIR__ . '/' . $imgNm);
After this the file is save in same directory as your script (named 'xyz.jpeg') and you can use it in whatever way you want
I added a line for SSL verification to be false and the code worked fine for me.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Hope this help someone.

How to get images from a site using curl

How do I get images from a specific site inside html content?
See the example here .
Here http://www.techtunes.com.bd/android-apps/tune-id/214272 is the main post link. I'm scraping content inside div#content using php. But images in the post give 403.
How do I show that image? I'm using the Goutte Library
use it like this: call the function with $url the image path, and $filename the image stored path.
private function httpgetfile($url, $filename)
{
$fp = fopen($filename, 'a+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
curl_close($ch);
fclose($fp);
return;
}

Get file contents without file_get_contents

I'm not allowed to use file_get_contents. Originally I got the file contents by simply doing this:
$filepath = $_FILES['resume-attachment']['tmp_name'];
$filecontent = file_get_contents($filepath);
$encodedFile = base64_encode($filecontent);
Unfortunately, it's not allowed.
$filepath = $_FILES['resume-attachment']['tmp_name'];
$filename = $_FILES['resume-attachment']['name'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $filepath);
$filecontent = curl_exec($ch);
curl_close($ch);
$encodedFile = base64_encode($filecontent);
The code above is my attempt at using cURL. I get the filename uploaded, so I get some reaction, but the uploaded file is 0.0 bytes of size... which is not correct. I would think that perhaps the issue could be that I shouldn't treat $filepath as a URL, but what would the alternative be? I should also mention that I'm not trying to post it from this code. I simply want to get the file contents, and then encode it. It's part of an XML string later on.

How can get an image from a 301 redirect download link in PHP?

I'm trying to download this image with PHP to edit it with GD. I found many solutions for image links, but this one is a download link.
Edit:
$curl = curl_init("http://minecraft.net/skin/Notch.png");
$bin = curl_exec($curl);
curl_close($curl);
$img = #imagecreatefromstring($bin);
This is my current code. It displays "301 Moved Permanently". Are there CURLOPTs I have to set?
$curl = curl_init("http://minecraft.net/skin/Notch.png");
// Moved? Fear not, we'll chase it!
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
// Because you want the result as a string
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$bin = curl_exec($curl);
curl_close($curl);
$img = #imagecreatefromstring($bin);
Here is an option to directly save the image to a file (instead of using imagecreatefromstring):
<?php
$fileName = '/some/local/path/image.jpg';
$fileUrl = 'http://remote.server/download/link';
$ch = curl_init($fileUrl); // set the url to open and download
$fp = fopen($fileName, 'wb'); // open the local file pointer to save downloaded image
curl_setopt($ch, CURLOPT_FILE, $fp); // tell curl to save to the file pointer
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // tell curl to follow 30x redirects
curl_exec($ch); // fetch the image and save it with curl
curl_close($ch); // close curl
fclose($fp); // close the local file pointer
fopen - depends on your php settings if url fopen is allowed.
or curl
see the fine php manual.

Categories