PHP image grab (w/o fopen?)

software development

#1

Okay, so we’re not allowed to use fopen functions with DH, so is there a way with like cURL to grab an image from somewhere to use it on another base image?

I’m not going to be getting weird Google images or random images from sites, but like …yes, it’s nerdy, to make forum signatures with the person’s avatar included. Nothing illegal. :stuck_out_tongue:

Please help? Thanks. :slight_smile:

:slight_smile:


#2

First, you can use fopen with local paths - just not with URLs. And yes, you can do it with curl. The Wiki is your friend, as it has all sorts of good information that will help you out.

–Matttail
art.googlies.net - personal website


#3

This post wasn’t helpful to me.

The url is on another site, as I had explained. PHP.net doesn’t have enough information. Wiki doesn’t have enough either.

Thanks for trying, though.

:slight_smile:


#4

First you know that cURL is used to grab content from other web sites already.
Second you know you need something that will manipulate images and image files.

PHP.net has a ton of documentation on cURL as well as examples. Not hard at all to whip up something that fetches a URL and save the contents to disk. Just search for “curl” in the “online documentation”

Same thing with image manipulation. Just search for “image” and you get Image Functions

So the answer to the question is “Yes”, and PHP.net is where you go to find out how to do it using PHP.

:cool: openvein.org -//- One-time [color=#6600CC]$50.00 discount[/color] on [color=#0000CC]DreamHost[/color] plans: Use ATROPOS7


#5

Nope. In my last post I mentioned PHP.net didn’t have the information I need.

I know this forum is ungodly slow, so it’s my last resort after Google and PHP.net fail me.

The closest thing I could find was a page that echos the entire site, which is not what I want.

I also tried using curl_getinfo, but all that came back was “Array.”

Nothing else looked helpful. I need a link to a page that is helpful for image cloaking, not rants on what to search for, please.

Thank you.

:slight_smile:


#6

[quote]I know this forum is ungodly slow

[/quote]

That’s what Simon’s Mom said too. :smiley:

Now I’m all fired up to help. :cool:

http://wiki.dreamhost.com/Special:Search?search=curl&fulltext=Search

----------
[color=#00CC00]ANONYMOUS0 => Max Discount[/color], [color=#6600CC]ANONYMOUS2 => Max Discount less $1[/color] or Cut the code, [color=#CC0000]it’s their birthday:[/color] [color=#00CC00]9999 => $99.99 Off[/color]


#7

Please realize we’re not being paid to help you here, you really have no reason to be demanding. Besides, if you can’t help your self enough to do a little searching and reading, how are you going to manage to write the necessary coding? So really, grow up and try to help your self - or pay someone to do it for you.

–Matttail
art.googlies.net - personal website


#8

No one’s demanding, please read my replies before judging. Thanks.

:slight_smile:


#9

Wiki editors never explain things the way they need to be enough to modify codes, I’m afraid.

I had this issue with CRON and just image manipulation in general. Coding topics on Wiki are always inefficient from my experience.

Like, there’s no real notes on the section of code, just general “this variable is for cookies, this line of code deletes a file” type of information.

How about another good PHP resource site? Any recommendations? Codewalkers.com also isn’t useful, as I’ve tried there a few times with nothing good.

I’m a slow learner, so I need examples with good notes.

:slight_smile:


#10

Seeing as nobody else could be bothered, I added a section to the wiki’s cURL article on getting binary data (with retrieving an image as the example). Once the binary data is assigned to a variable, you can start processing it.


si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#11

Well, thank you very much. :slight_smile:

I’ll be checking it out in a minute or so.

:slight_smile:


#12

You are most welcome.


si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#13

Well recently I ran into the same problem after I wrote a php transloading script.

So I used your example and its first time with curl. And I think I understand it. Ive come up with this.

<?php $image_url = "$_POST[sourceurl]"; $ch = curl_init(); $timeout = 0; curl_setopt ($ch, CURLOPT_URL, $image_url); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // Getting binary data curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); $thefile = curl_exec($ch); curl_close($ch); // output to browser header("Content-type: image/jpeg"); print $thefile; } ?>

File:

I have tried adding this and removing the output to browser code

$image_url = “$_POST[sourceurl]”;
$ext = substr($image_url, -3, 3);
$ch = curl_init();
$timeout = 0;
$fp = fopen("$image_url", “w”);
curl_setopt ($ch, CURLOPT_URL, “temp” $image_url.".".$ext );
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($ch, CURLOPT_FILE, $fp);

But im having problem saving it . Instead of printing I want to save it then run it through another script. My problem is the saving part. And also if I can get it to save how would I pass it to my other script. I dont know why having such hard time with this, but it refuses to work. If you can direct me where to look or help me out id greatly apreciate it.


#14

Hmmm. There are a few things going wrong there. Try this, and let me know how it works out (because I haven’t checked it):

[code]<?php

// get image
$image_url = $_POST[‘sourceurl’];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $image_url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$thefile = curl_exec($ch);
curl_close($ch);

// get filename
$path = explode("/", $image_url);
$dir = “/images”; // your destination directory - must have write permissions
$filename=$dir."/".$path[count($path)-1];

// save image
if(is_writable($filename)) {
if(!fp = fopen($filename, “a”)) {
echo “Cannot open file ($filename)”;
exit;
}
if(fwrite($fp, $thefile) === FALSE) {
echo “Cannot write to file ($filename)”;
exit;
}
echo “Success, wrote to file ($filename)”;
fclose($fp);
} else {
echo “The file $filename is not writable”;
}
?>[/code]--------
si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#15

No thats not working. Thank You for trying though. I tried something similar to yours. But I still cant get it to work.

I have been working on this problem for couple days now.

After I was told

URL file-access is disabled

For security reasons on dreamhost

So is why im trying to figure out Curl.

This is the code I wanna use

<? // get variables for fields on upload screen $thefile = $_FILES['thefile']; $currentip = $_SERVER['REMOTE_ADDR']; $messages = ""; // check for blocked ip address if ($currentip != "") { $link = mysql_connect($db_server, $db_user, $db_password) or die("Could not connect to the database."); mysql_select_db($db_name) or die("Could not select the database."); $query = "select ip from blocked where ip = '$currentip'"; $result = mysql_query($query) or die("Query failed."); $num_rows = mysql_num_rows($result); if ($num_rows > 0) { $messages .= "Your IP address (" . $currentip . ") has been blocked from using this service. "; } } // check for a file if (!isset($thefile) || $thefile['name'] == "") { $messages .= "You must select a file to upload using the "Browse" button. "; } if ($messages == "") { // check for valid file type if (!in_array_nocase($thefile['type'], $valid_mime_types)) { $messages .= "" . $thefile['name'] . " is not in a valid format (" . $valid_mime_types_display . "). "; } // check for valid file extension $thefile['name'] = strtolower($thefile['name']); $file_ext = substr($thefile['name'], strlen($thefile['name']) - 3, strlen($thefile['name'])); // fix for JPEG names files if ($file_ext == "peg") { $file_ext = "jpg"; } if (!in_array_nocase($file_ext, $valid_file_ext)) { $messages .= "" . $thefile['name'] . " (".$file_ext.") does not have a valid file extension (" . $valid_mime_types_display . "). "; } // check for valid file size if ($thefile['size'] > ($max_file_size_kb * 1024)) { $messages .= "" . $thefile['name'] . " is bigger than the allowed file size of " . $max_file_size_kb . " K. "; } } // save the file, if no error messages if ($messages == "") { $max_file_size_byes = $max_file_size_kb * 5024; // SAVE THE PICTURE $newFileName = newImageName($thefile['name']); $newFile = $server_dir . $newFileName; $newFileUrl = $image_url . $newFileName; $newFileUrlLink = $server_save_directory . $newFileName; if ($file_ext == "jpg" || $file_ext == "png" || $file_ext == "gif") { $tnFileName = "tn_" . substr($newFileName, 0, strlen($newFileName) - 3) . "jpg"; $tnFile = $server_dir . $tnFileName; $tnFileUrl = $image_url . $tnFileName; } else { $tnFileName = ""; $tnFile = ""; $tnFileUrl = ""; } $filesize = $thefile['size']; $newID = ""; if (!@copy($thefile['tmp_name'], $newFile)) { $messages .= "An unexpected error prevented your file from being saved. Please try again."; } else { // add to database $query = "INSERT INTO images (filename, tn_filename, filepath, ip, filesize) VALUES ('$newFileName', '$tnFileName', '$server_save_directory', '$currentip', $filesize)"; mysql_query($query) or die("Database entry failed."); $newID = mysql_insert_id(); } if ($file_ext == "jpg" || $file_ext == "png" || $file_ext == "gif") { if ($file_ext == "jpg") { $source_id = imagecreatefromjpeg($newFile); } elseif ($file_ext == "png") { $source_id = imagecreatefrompng($newFile); } elseif ($file_ext == "gif") { $source_id = imagecreatefromgif($newFile); } $true_width = imagesx($source_id); $true_height = imagesy($source_id); // create thumb if ($true_width > $thumbnail_size_max || $true_height > $thumbnail_size_max) { if ($true_width >= $true_height) { $dest_width = $thumbnail_size_max; $dest_height = ($dest_width / $true_width) * $true_height; } else { $dest_height = $thumbnail_size_max; $dest_width = ($dest_height / $true_height) * $true_width; } if ($thefile['type'] == "image/jpeg" || $thefile['type'] == "image/pjpeg") { $target_id = imagecreatetruecolor($dest_width, $dest_height); } elseif ($thefile['type'] == "image/png" || $thefile['type'] == "image/x-png") { $target_id = imagecreatetruecolor($dest_width, $dest_height); } else { $target_id = imagecreatetruecolor($dest_width, $dest_height); } $target_pic = imagecopyresized($target_id, $source_id, 0, 0, 0, 0, $dest_width, $dest_height, $true_width, $true_height); // create a thumbnail in JPEG format imagejpeg($target_id, $tnFile, $thumbnail_quality); imagedestroy($target_id); } else { copy($newFile, $tnFile); } } } mysql_close($link); // create URL links to display to user $showURL1 = false; // image on hosted page - image only $showURL2 = false; // direct link to file - all $showURL3 = false; // HTML for img - image only $showURL4 = false; // [img][/img] tags - image only $showURL5 = false; // thumbnail pic - image only // determine flags $showURL2 = true; if ($file_ext == "jpg" || $file_ext == "gif" || $file_ext == "png" || $file_ext == "bmp") { $showURL1 = true; $showURL3 = true; $showURL4 = true; } if ($file_ext == "jpg" || $file_ext == "gif" || $file_ext == "png") { $showURL5 = true; } function newImageName($fname) { $timestamp = time(); $new_image_file_ext = substr($fname, strlen($fname) - 3, strlen($fname)); if ($new_image_file_ext == "peg") { $ext = ".jpg"; } else { $ext = "." . $new_image_file_ext; } $newfilename = randString() . substr($timestamp, strlen(timestamp) - 4, strlen(timestamp)) . $ext; return $newfilename; } function randString() { $newstring=""; while(strlen($newstring) < 3) { $randnum = mt_rand(0,61); if ($randnum < 10) { $newstring .= chr($randnum 48); } elseif ($randnum < 36) { $newstring .= chr($randnum 55); } else { $newstring .= chr($randnum 61); } } return $newstring; } function in_array_nocase($item, $array) { $item = &strtoupper($item); foreach($array as $element) { if ($item == strtoupper($element)) { return true; } } return false; } ?>

I just cant figure out how to bridge the curl to it. Maybe if was someway could save the output of the curl code to a temp file as variable thefile. Then from there run it through my code. I dont know but any help is appreciated


#16

There’s at least one bug in my example:

if(!fp = fopen($filename, "a")) {should be

if(fopen($filename, "a") === FALSE) {I just don’t have the time to give it any serious consideration at the moment.


si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


#17

Yeah I found that one right away. But even fixing it doesnt fix the code. But if you re read the post above your last one thats what im trying to do.

Got an Image? Host it Here!! http://www.imageverse.org


#18

Just to let you know I have come to the conclusion there is no transloading on Dreamhost… Grabbing the image is one thing…Which is easy enough to do… But then saving it and passing it through other script… NO WORKIE :(…I tried everything…without Fopen its a pain

Got an Image? Host it Here!! http://www.imageverse.org


#19

So where are you getting hung up? Once you can “save” it, then you can use fopen to work with it by referencing its via a filesystem path instead of a url - if you are familiar with fopen, then give it a shot that way :wink: .

fopen works on DreaHost - it is only allow_url_fopen that is disabled - don’t use the URL once you have grabbed the image using cURL.

There is nothing about DreamHost that prevents “transloading” - it’s just a programming problem that has you frustrated.

–rlparker


#20

I ran ( Iran :wink: ) across the same problem: My Host does not allow fopen() they told me to use cURL instead of fopen, so here i go…

i got this nice function getUrl() from http://nadeausoftware.com/articles/2007/06/php_tip_how_get_web_page_using_curl

and produced this:

<?php /** * Get a web file (HTML, XHTML, XML, image, etc.) from a URL. Return an * array containing the HTTP server response header fields and content. */ function getUrl( $url ) { $ch = curl_init( $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );// return web page curl_setopt( $ch, CURLOPT_HEADER, false );// don't return headers curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );// follow redirects curl_setopt( $ch, CURLOPT_ENCODING, "" );// handle all encodings curl_setopt( $ch, CURLOPT_USERAGENT, "spider" );// who am i curl_setopt( $ch, CURLOPT_AUTOREFERER, true );// set referer on redirect curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 120 );// timeout on connect curl_setopt( $ch, CURLOPT_TIMEOUT, 120 );// timeout on response curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );// stop after 10 redirects $content = curl_exec( $ch ); $err = curl_errno( $ch ); $errmsg = curl_error( $ch ); $header = curl_getinfo( $ch ); curl_close( $ch ); $header['errno'] = $err; $header['errmsg'] = $errmsg; $header['content'] = $content; return $header; } $header=getUrl($domain); $imageFile = $header["content"]; $im = imagecreatefromstring ($imageFile); ?>

seems to work ;-)…