0

I have gotten the code to take an image from android and save it to my server, however I need to resize the image to a standard size. I have looked around and have not been able to get anything to work. Below is my code so far.

<?php
    // Get image string
    $base = $_REQUEST['image'];

    // Get file name
    $filename = $_REQUEST['filename'];

    // Decode Image
    $binary = base64_decode($base);
    header('Content-Type: bitmap; charset=utf-8');

    // Images will be saved
    $file = fopen('/home1/skyrealm/public_html/img/'.$filename.'.jpg', 'wb');
    if($file != true)
    {
        echo'Error uploading image';
    }
    else
    {
        // Create/Svae File
        fwrite($file, $binary);
        fclose($file);
        echo 'Image upload successful';
    }
?>

EDIT:

This is the code android side:

public void encodeImagetoString() {
    new AsyncTask<Void, Void, String>() {

        protected void onPreExecute() {

        };

        @Override
        protected String doInBackground(Void... params) {
            BitmapFactory.Options options = null;
            options = new BitmapFactory.Options();
            options.inSampleSize = 3;

            bitmap = BitmapFactory.decodeFile(imgPath, options);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();

            // Must compress the Image to reduce image size to make upload easy
            bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
            byte[] byte_arr = stream.toByteArray();

            // Encode Image to String
            encodedString = Base64.encodeToString(byte_arr, 0);
            return "";
        }

        @Override
        protected void onPostExecute(String msg) {

            // Put converted Image string into Async Http Post param
            params.put("image", encodedString);

            // Trigger Image upload
            triggerImageUpload();
        }
    }.execute(null, null, null);
}
borracciaBlu
  • 4,017
  • 3
  • 33
  • 41
Bytesized
  • 133
  • 1
  • 15
  • Maybe you want sth like this http://php.net/manual/en/imagick.resizeimage.php , although you still need to save it first! – Roberto Anić Banić Jul 24 '15 at 02:14
  • Why do you want to upload the full size image from Android app to server and then resize using php? It's a bad practice, cos it consumes more band-width for both app users and your server. Rather, resize the image from the app, then upload to server and just save using php. – Adnan Jul 24 '15 at 02:15
  • Dont you like to resize it using android's Bitmap Factory – Sheychan Jul 24 '15 at 02:16
  • added my android code. Still not sure how to permanently resize the image. – Bytesized Jul 24 '15 at 02:27
  • Why are you sending it as base64? Also why are you setting a header for images if you're echoing text out? – Anonymous Jul 24 '15 at 02:44
  • base 64 as its social and wanted everything private. header because i thought that was necessary – Bytesized Jul 24 '15 at 02:51

1 Answers1

1

As said in the comments you dont need header('Content-Type: bitmap; charset=utf-8'); if you don't came out with the image itself.

About the resize the script is more or less fine just use Imagemagic or the GD library .

Some post about resizing:
php - resize and save an image?

Community
  • 1
  • 1
borracciaBlu
  • 4,017
  • 3
  • 33
  • 41