0

Is there any way to save received image in two places , One in original size in a folder and the other one in thumbnail size (90 , 120) in another folder using PHP . Aspect ratio of original images are (3 , 4) and I just need to change the size ...
Already my code is doing good and can save image in original size .
By the way images are sending from an android app.
here is the php code to save image in folder and insert data to table of database ...

<?php

    if ($_SERVER["REQUEST_METHOD"]=="POST") {
        require 'connection.php';
        insertData();
    }

    function insertData(){
        global $connect;

        mysqli_set_charset($connect,"utf8");

        $name = $_POST["name"];
        $description = $_POST["description"];
        $image = $_POST["image"];

        $sql ="SELECT id FROM my_table ORDER BY name ASC";

        $res = mysqli_query($connect,$sql);

        $id = 0;

        while($row = mysqli_fetch_array($res)){
                $id = $row['id'];
        }

        $path = "img/$id.png";

        $decoded_string = base64_decode($image);

        $file = fopen($path, 'wb');

        $is_written = fwrite($file, $decoded_string);
        fclose($file);

        $imgpath = "http://my-site.com/folder/$path";

        if($is_written > 0) {

            $query = "INSERT INTO my_table (path,name,description) VALUES ('$imgpath','$name','$description ')";

            $result = mysqli_query($connect, $query) ;

            if($result){
                echo "success";
            }else{
                echo "failed";
            }

            mysqli_close($connect);
        }
    }
?>
IanS
  • 15,771
  • 9
  • 60
  • 84
unknown
  • 29
  • 1
  • 7

3 Answers3

1

Try this:

<?php

if ($_SERVER["REQUEST_METHOD"]=="POST") {
    require 'connection.php';
    insertData();
}

function insertData(){
    global $connect;

    mysqli_set_charset($connect,"utf8");

    $name = $_POST["name"];
    $description = $_POST["description"];
    $image = $_POST["image"];

    $sql ="SELECT id FROM my_table ORDER BY name ASC";

    $res = mysqli_query($connect,$sql);

    $id = 0;

    while($row = mysqli_fetch_array($res)){
            $id = $row['id'];
    }

    $path = "img/$id.png";
    $path_thumb = "thumb/$id.png";


    $decoded_string = base64_decode($image);

    $file = fopen($path, 'wb');

    $is_written = fwrite($file, $decoded_string);
    fclose($file);
    $jpg_image = imagecreatefromjpeg($path);

    $orig_w = imagesx($jpg_image);
    $orig_h = imagesy($jpg_image);
    $new_w = 90;
    $new_h = round ( (90 * $orig_h) / $orig_w);

    $new_image = imagecreatetruecolor($new_w, $new_h);
    imagecopyresampled($new_image, $jpg_image, 0, 0, 0, 0, $new_w, $new_h,$orig_w,$orig_h);
    imagejpeg($new_image,$path_thumb,80);


    $imgpath = "http://my-site.com/folder/$path";

    if($is_written > 0) {

        $query = "INSERT INTO my_table (path,name,description) VALUES ('$imgpath','$name','$description ')";

        $result = mysqli_query($connect, $query) ;

        if($result){
            echo "success";
        }else{
            echo "failed";
        }

        mysqli_close($connect);
    }
}
?>

Do not forget create folder 'thumb' near the 'img' folder. And this example is for jpeg images. If you need to post png file you need to call imagecreatefrompng instead of imagecreatefromjpeg;

Ivnhal
  • 1,099
  • 1
  • 12
  • 20
0

You need GD library installed on your server. Just echo phpinfo(); and check if its module is enabled.

Then: (I can't test now, sorry.)

list($width, $height) = getimagesize($imgpath);
$source = imagecreatefromjpeg($imgpath);
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);/

imagecopyresized($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);

imagejpeg($thumb, $path_to_thumb);

We get size of the original image and its content, then we create a empty true color image. We copy source content to the empty image, passing both thumb and original size. So, we save the thumbnail, passing the $thumb image and where it is gonna be saved.

0

So I find the answer, just a little different from stweb answer
here is the code

<?php

if ($_SERVER["REQUEST_METHOD"]=="POST") {
    require 'connection.php';
    insertData();
}

function insertData(){
    global $connect;

    mysqli_set_charset($connect,"utf8");

    $name = $_POST["name"];
    $description = $_POST["description"];
    $image = $_POST["image"];

    $sql ="SELECT id FROM my_table ORDER BY name ASC";

    $res = mysqli_query($connect,$sql);

    $id = 0;

    while($row = mysqli_fetch_array($res)){
            $id = $row['id'];
    }

    $path = "img/$id.png";
    $path_thumb = "thumb/$id.png";


    $decoded_string = base64_decode($image);

    $file = fopen($path, 'wb');

    $is_written = fwrite($file, $decoded_string);
    fclose($file); 

    $imgpath = "http://my-site.com/folder/$path";


    //---------------- create thumbnail ----------------

    $jpg_image = imagecreatefromjpeg($path);

    $orig_w = imagesx($jpg_image);
    $orig_h = imagesy($jpg_image);
    $new_w = 120;
    $new_h = 90;

    $new_image = imagecreatetruecolor($new_w, $new_h);
    imagecopyresampled($new_image, $jpg_image, 0, 0, 0, 0, $new_w, $new_h,$orig_w,$orig_h);
    imagejpeg($new_image,$path_thumb);

    $thumbpath = "http://my-site.com/folder/$path_thumb";

    //---------------- create thumbnail ----------------


    if($is_written > 0) {

        $query = "INSERT INTO my_table (path,thumbnail,name,description) VALUES ('$imgpath','$thumbpath','$name','$description ')";

        $result = mysqli_query($connect, $query) ;

        if($result){
            echo "success";
        }else{
            echo "failed";
        }

        mysqli_close($connect);
    }
}
?>
unknown
  • 29
  • 1
  • 7