Open File Run API Save File Open Next File Repeat Stop

Hello everyone,

Currently looking at building my first PHP function that will do exactly what the title says.

Step 1) Open the first img file in a directory
Step 2) Run an image optimisation API from an external server or local git copy
Step 3) Over write the current file
Step 4) Repeat until all files have been checked
Step 5) Exit the function and display all the results on the front end

Now this sounds really simple when I lay it out in 5 steps, but being a total beginner I’m not sure where to start.

Should I be looking for snippets of code in a PHP Cookbook and trying to combine these all together. Is an objective method the way to go?

Any tips would be great

Where do you start? Exactly where you are.

You’ve successfully broken down the process into it’s component steps.

Now take each step, and convert it into code.
The hardest part will be the external API; as i presume you’re going to have to POST the image data to it. [cURL is your buzzword here]

Step 4 is to wrap steps 1-3 in a while. Step 5 is to add data collection to the whole thing for returning. [Generally, functions dont write to the screen directly]

Hi Star Lion,

Thanks for your reply.

That definitely gives me a starting point.

I will report back soon with my outcomes

Ok so this is an example of compressing one image with the API

<?php
define('WEBSERVICE', 'http://www.resmush.it/ws.php?img=');
$s = 'http://www.mywebsite/image.jpg';
$o = json_decode(file_get_contents(WEBSERVICE . $s));

if(isset($o->error)){
die('Error');
}
echo $o->dest; //URL of the optimized picture

I’m looking to open every image in my image directory folders which are:

“/img/p” or “/img/s” or “/img/scenes” or “/img/st” or “/img/su” or “/img/t” or “/img/tmp”

I’m thinking maybe

function imageapi_optimize_services_resmushit($image, $dst)
if (!function_exists('json_decode')) {
echo(t('required function, json_decode(), is not available.'), 'error');
return FALSE;
}
$dst = 'img/p'
$url = 'http://resmush.it/ws.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('files' => '@' . $dst));
$data = curl_exec($ch);
curl_close($ch);
$json = json_decode($data);

Or something along those lines

Okay if the API does external fetching instead of requiring you to upload the file, you wont need cURL at all - file get contents would suffice.

So based on your example, and what you’ve said, here’s the revised set of steps your program should take.

Create Blank Output Array
Start While On Different Directories (Step 4a)
Get Current Directory Listing [Either a scandir or a glob]
Start Foreach on list of filenames (Step 4b)
Manufacture external URL (“$s” in your example) of current file (Step 1)
Send created URL to external API (Step 2)
Get Contents of post-API image from URL contained in JSON returned and save it (Step 3)
Generate output message and store to array (Step 5)
EndForeach
EndWhile
Return Ouput Array

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.