unknown unknown - 3 months ago 10
PHP Question

How to save received image in two places, one in original size and the other in thumbnail size using PHP

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);
}
}
?>

Answer

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);
    }
}
?>