0

i am beginner and i have been trying to reduce the image size on upload but failing to get results. Please help me. Here is my Php code

<?php
if (isset($_POST['update'])){
header('Location:index2.php');  
$upload_image = $_FILES["image"][ "name" ];
$folder = "images/";
move_uploaded_file($_FILES["image"]["tmp_name"], "$folder".$_FILES["image"]["name"]);
$file = 'images/'.$_FILES["image"]["name"];
$uploadimage = $folder.$_FILES["image"]["name"];
$newname = $_FILES["image"]["name"];
$random = substr(number_format(time() * rand(),0,'',''),0,10);
$resize_image = $folder.$newname.$random; 
$actual_image = $folder.$newname;

list( $width,$height ) = getimagesize( $uploadimage );

$newwidth = 600;

$newheight = 600;

$thumb = imagecreatetruecolor( $newwidth, $newheight );
$source = imagecreatefromjpeg($actual_image);

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg( $thumb, $resize_image, 60 ); 

$out_image=addslashes(file_get_contents($resize_image));


$userid = $_SESSION['id'];
$date = $_POST['date'];
$data = mysqli_query($db, "SELECT `image` FROM `posts` WHERE id = '$userid' ");
$sql = "insert into posts (userid, image, date) values ('$userid', '$image', '$date')";
mysqli_query($db, $sql);



}

?>

If i use this code i am getting the image rotated and also image is not decreasing.

Imr An
  • 27
  • 7
  • 2
    You are wide open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php) and should really use [Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead of concatenating your queries. Specially since you're not escaping the user inputs at all! – M. Eriksson Aug 14 '17 at 15:03
  • 3
    _"but failing to get results"_ - As far as I can see, you're not trying to reduce the image quality at all, so I'm curious about what results you're expecting? – M. Eriksson Aug 14 '17 at 15:04
  • There's not actually anything in there to manipulate the image... you probably want to look at the [GD library](http://php.net/manual/en/book.image.php) or possibly [ImageMagick](http://php.net/manual/en/book.imagick.php) if it's running on your server (unlikely tbh) – CD001 Aug 14 '17 at 15:04
  • Why do you have `header('Location:index2.php');` in the top of your script? – M. Eriksson Aug 14 '17 at 15:05
  • As i said i am totally beginner to Php. I am not getting a proper code to reduce image size. and i am using header('Location:index2.php'); to display the results in index2.php. – Imr An Aug 14 '17 at 15:10
  • @Magnus Erikson i have tried many codes to reduce image size but failing to achieve results. As far as sql injections i have just posted a code just for understanding purpose – Imr An Aug 14 '17 at 15:13
  • 1
    Where are you trying to reduce the image size? Hint: get the dimension of the image you just uploaded, create a new image in memory, copy the data to that image, and write it out to disk at a new compression level (or at reduced dimensions). Will require the PHP GD library. If you're on Ubuntu, you'll need to `sudo apt-get install php7.0-gd` to install it on your system. – Lucas Krupinski Aug 14 '17 at 15:17
  • @ Lucas i have been trying on different codes but just posted a simple code in order to get some help. Will try your suggestion. Thanks – Imr An Aug 14 '17 at 15:19
  • You need to show us what you've tried. We're happy to help you with your _existing_ code, but we won't write it for you. – M. Eriksson Aug 14 '17 at 15:21
  • Sure i will edit the code @ Magnus – Imr An Aug 14 '17 at 15:21
  • I have edited the code @MagnusErikson – Imr An Aug 14 '17 at 15:32
  • 1
    You should strongly consider using a class like http://image.intervention.io/ for this. – ceejayoz Aug 14 '17 at 15:34
  • Thanks for the suggestion @ceejayoz i will try it now – Imr An Aug 14 '17 at 15:35

0 Answers0