PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 10, 2022

[FIXED] How to resize image without storing it on a folder

 October 10, 2022     gd, image, image-resizing, php     No comments   

Issue

So, i have been stuck on this for couple of hours. I have tired to google any solutions for this but didn't find solutions. So any help is greatly appreciated.

My Issue:

I am using form to upload multiple images. Due to data being backup on multiple servers i have to stored them on database. And storing the images on folder is not a solution. I thinks I can save the image onto the database but my I need to create a thumbnail of all the images which are being uploaded. So that's my problem, how do i create a thumbnail from those tmp files. I created to use imagecreate its not working, I am not able to fetch the content of that thumbnail and save it onto the database.

This is a code i used to resize that image which doesn't return the contents.

 function resize_image($file, $w, $h, $crop=FALSE) {
    list($width, $height) = getimagesize($file);
    $r = $width / $height;
    if ($crop) {
        if ($width > $height) {
            $width = abs(ceil($width-($width*abs($r-$w/$h))));
        } else {
            $height = abs(ceil($height-($height*abs($r-$w/$h))));
        }
        $newwidth = $w;
        $newheight = $h;
    } else {
        if ($w/$h > $r) {
            $newwidth = $h*$r;
            $newheight = $h;
        } else {
            $newheight = $w/$r;
            $newwidth = $w;
        }
    }
    $src = imagecreatefromjpeg($file);
    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    return $dst;
}

This is my code which is used after form is posted: Right now it only displays all the files that are uploaded.

 for($i=0;$i< count($_FILES['upload_file']['tmp_name']);$i++)
 {

        $size = getimagesize($_FILES['upload_file']['tmp_name'][$i]);
        $name = $_FILES['upload_file']['name'][$i];
        $type = $size['mime'];          
        $file_size_bits = $size['bits'];
        $file_size_width = $size[0];
        $file_size_height = $size[1];
        $name = $_FILES['upload_file']['name'][$i];
        $image_size = $size[3];
        $uploadedfile = $_FILES['upload_file']['tmp_name'][$i];
        $tmpName  = base64_encode(file_get_contents($_FILES['upload_file']['tmp_name'][$i]));
        $sImage_ = "data:" . $size["mime"] . ";base64," . $tmpName;
        echo '<p>Preview from the data stored on to the database</p><img src="' . $sImage_ . '" alt="Your Image" />';


    }

I need to create a thumbnail of those files that are being uploaded. How do i achieve that.

Please advise.

Thanks for your help.

Cheers


Solution

This is your big problem:

return $dst;

$dst is the image resource, not the image data.

You should use imagejpeg() or imagepng() to send the image data back instead.

Because those functions output the data stream to browser, we use some output buffer functions to capture the outputted image data instead of send it to the browser.

So,

return $dst;

replace with:

ob_start();
imagejpeg( $dst, NULL, 100); // or imagepng( $dst, NULL, 0 );
$final_image = ob_get_contents();
ob_end_clean();
return $final_image;


Answered By - Patrick Moore
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing