-2

I want to do is to resize every image to 300x300. My problem is in my current code some of the image file that was move to the upload folder are to big. I want all image file that is in the upload folder to be size 300x300.

current php code:

<?php
include_once('../dbc/database.php');

$db = new Connection();
$db = $db->dbConnect();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$emailCodeResult = isset($_POST['emailCodeResult']) ? $_POST['emailCodeResult'] : "";

$imageLink = isset($_POST['imageLink']) ? $_POST['imageLink'] : "";
const path = "Oppa/upload/";
$s= explode(path,$imageLink);
unlink("../upload/".$s[1]);

$email = isset($_POST['email']) ? $_POST['email'] : "";

$type = $_FILES["imageInput"]["type"];
$ext = end(explode('/', $type));
$filename = uniqid() . '_' .$emailCodeResult . '.' . $ext; 
move_uploaded_file($_FILES["imageInput"]["tmp_name"], "../upload/" . $filename);
$location = "Oppa/upload/" . $filename;

if(!empty($_POST['email'])) {

        $q = "UPDATE tbl_user SET user_image = '$location' WHERE user_email= :email ";
        $query = $db->prepare($q);
        $query->bindParam(':email', $email);
        $results = $query->execute();
        echo "1";

}
?>
  • Please Google before posting any questions. There are THOUSANDS of tutorials that explain how to resize images either using [ImageMagick](http://php.net/manual/en/book.imagick.php) or the [PHP GD](http://php.net/manual/en/book.image.php) functions. – David Jul 20 '14 at 06:35

1 Answers1

0

Here is the code to resize image. May this help you

             $path = getcwd();
             $oldpic = $path.'/images/test.jpg'; //your image path
             $array = explode("/",$oldpic);
             $count = count($array);
             $name = $array[$count-1];

             $src = $oldpic;
             $dest = $path."/images/thumbnail/".$name; // resized image

             //Genrating the image from there extension
             $size = getimagesize($src);
             switch($size["mime"]){

                        case "image/jpeg":
                          $source_image = imagecreatefromjpeg($src); //jpeg file
                        break;

                        case "image/gif":
                          $source_image = imagecreatefromgif($src); //gif file
                        break;

                        case "image/png":
                          $source_image = imagecreatefrompng($src); //png file
                        break;

                        default:
                          $source_image=false;
                        break;
             }
             $width = imagesx($source_image);
             $height = imagesy($source_image);
             $newwidth=300;
             $newheight= 300;
             $virtual_image = imagecreatetruecolor($newwidth, $newheight);
             imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
             imagejpeg($virtual_image,$dest,100);
Rakesh kumar
  • 135
  • 6