Possible to use GD (of image manipulation) with my slim restful api?

#1

Hi everyone.
First of all, i’m new user of slim framework.

So i want to create a restful api for an android app (with java).

I want that my api without the CRUD database operations doing someting like:

-Génerate an image wich will be displayed in the mobile app
-Generate an excel (.xls) file as statistic downloadable by my android app user.

Is possible ?

Thank for your help

0 Likes

#2

mistake: (I want that my api without the CRUD database operations doing someting like)

I mean with the CRUD operation…, my api must perform these action:
-Génerate an image wich will be displayed in the mobile app
-Generate an excel (.xls) file as statistic downloadable by my android app user.

Is possible ?

Thank for your help
:

0 Likes

#3

I have interpreted your question so that you are looking for a way to create image/excel-files and download them automatically.

To create Excel files I would recommend: PhpSpreadsheet.
Here is an blog post that explains how you can generate and download Excel files with Slim.

PHP itself provides a lot of built-in functions to generate images like jpg, png and so on. Returning an image as response could be done like this:

Example:

use Slim\Http\Request;
use Slim\Http\Response;

$app->get('/image', function (Request $request, Response $response) {
    $image = imagecreate(200, 80);
    imagecolorallocate($image, 255, 255, 255);
    $textColor = imagecolorallocate($image, 113, 158, 64);
    $lineColor = imagecolorallocate($image, 170, 170, 170);
    imagestring($image, 8, 35, 25, 'slim framework', $textColor);
    imagesetthickness($image, 2);
    imageline($image, 35, 45, 160, 45, $lineColor);

    ob_start();
    imagepng($image);
    $image = ob_get_clean();

    $response->write($image);

    return $response->withHeader('Content-Type', 'image/png');
});
0 Likes

#4

OK,thank i will try it an inform you.

Cordially.

0 Likes

#5

I don’t pretend for single source of truth and don’t know, sorry, your level of expierence, but I’ll try to give some advices.

  1. Maybe it’s better to keep statistic data in database (DB). One can get the data of any time period. As for image, I’d advise to store image-file on server, but reference to the image-file in the DB. Let’s, for example, use MySQL. But it’s matter of DB design for your project.
  2. To get the data from the DB you should make two main parts of work: backend (use Slim PHP framework) and frontend (this is Android).
  3. Slim PHP framework handles queries to DB and sends responses to Android in format of JSON.
  4. Android-app with such tools like Retrofit (HTTP-client for Java and Android http://square.github.io/retrofit/) and Picasso http://square.github.io/picasso/ or Glide https://github.com/bumptech/glide (libraries that helps to load image from server to Android-app) can get data from server (text and image, accordingly). To be correct, those image libs load references, because Android-app should not be over-bloated with image-files.
  5. To present statistic data, take a look for MPAndroidChart https://github.com/PhilJay/MPAndroidChart and Room https://developer.android.com/topic/libraries/architecture/room. Server data recieved from server can be cached in Android-app with Room, after that Android-app can get data (image-file reference also) from local database (I mean SQLite that is embeded in Android).
  6. As for Excel-file in Android-app. I’m apologising, but to present the statistic data in such way it’s weird for Android-app. Please, use Android’s tools and ways to get and to show data to your Android-app’s users.

I hope it will help you.

0 Likes

#6

Yes, thank so much for your help.
It’s very helpful.

One question please:
When i finish my app developpment, give me the different step to migrate the app to online server.

Cordially

0 Likes

#7

Well. I will try to explain as clearly as possible.

  1. Make an API for your Android-app with Slim 3. For preliminary testing the API you can use local-server and Postman https://www.getpostman.com/.
  2. The main purpose for the Postman is to test your server requests and responses. To evaluate those requests and responses (designed by you, by the way) you have to understand how Retrofit handles them. I think Retrofit’s docs are clear enough and there are a lot of tutorials in a net. Android-app and server should comunicate in JSON format (at least, in our case).
  3. In Android-app you should design models (POJO classes) so that they can handle (fit to) the structure of the server responses. And to test it - the Postman for help.
  4. If everything is OK, just put your server part (the API, your Slim-project) to real (I mean not local) server with FTP-client (FileZilla or WinSCP).
  5. In Android-app you should specify correct path to those resources (I mean you PHP-files, that handles requests and produces responses). For example, www.yourdomain.com/myproject/someapi/ . And that will be a BASE_URL for Retrofit (again, see Retrofit’s docs).

I think it will help you.

0 Likes