Edit and crop uploaded image
Asked Answered
S

2

1

Can you help me to edit this code?

function insert_lenses(){
    // to get userid
  $reg=$_SESSION['myusername'];
  $result_users = mysql_query("SELECT * FROM users WHERE user_name='$reg'");

  while($row_users = mysql_fetch_array($result_users))
    {
    $uid=$row_users[id];
    }

  $uploader = $_POST['uploader'];
  $path = 'photos/';
  $image=$_FILES['img_name'];
  $img_title=$_POST['title'];
  $img_tag=$_POST['tags'];
  $img_desc=$_POST['description'];
  $img_status=$_POST['status'];
  $lenses_id=$_POST['lenses'];
  $cam_id=$_POST['cams'];
  $date = date("d.m.Y");
  //------------------------------------------
  $image_size=$_FILES['img_name']['size'];
  $filename = stripslashes($_FILES['img_name']['name']);
  $extension = getExtension($filename);
  $extension = strtolower($extension);

  $image_name=time().'.'.$extension;
  $newname=$path.$image_name;
  $copied = copy($_FILES['img_name']['tmp_name'], $newname);


  if ($copied) {
    $sql=mysql_query("insert into images (uid, lid, imageurl, img_date, imagesize, imagedesc, imagetitle, imagetag, status,cam,lens,user_name,img_w,img_h)
VALUES('$uid','$lenses_id','$newname','$date','$image_size','$img_desc','$img_title','$img_tag','$$img_status','$cam_id','$lenses_id','$uploader','$w','$h')");



    return true;
  }else{
    echo "<center><h3>There are An Errors In Uploading!</h3></center>";
    return false;
  }
}


function getExtension($str) {
  $i = strrpos($str,".");
  if (!$i) { return ""; }
  $l = strlen($str) - $i;
  $ext = substr($str,$i+1,$l);
  return $ext;
}

$reg=$_SESSION['myusername'];
$result_users = mysql_query("SELECT * FROM users WHERE user_name='$reg'");

while($row_users = mysql_fetch_array($result_users))
  {
  $getid=$row_users[id];
  }

I need to add in this function RESIZE AND CROP IMAGE Like flickr and facebook img, I made a new folder: /img_croped

I want to insert the new image in this folder with ( resize and crop 58*58px )

and :

 mysql_query("insert into images (small_img) VALUES('$croped')"); // URL VALUE
Swimmingly answered 10/3, 2011 at 4:21 Comment(2)
What are the possible image types?Kastroprauxel
Beyond the obvious SQL injection vulnerabilities, don't use the size, name, and type values from the $_FILES array - they're user-provided and can be subverted. As well, don't use copy. There's a move_uploaded_file function specifically to deal with file uploads.Rothman
N
2

To resize and crop image,check below code which helps to crop image:

<?php
   if( isset($_POST['submit']) ) {
      include('SimpleImage.php');
      $image = new SimpleImage();
      $image->load($_FILES['uploaded_image']['tmp_name']);
      $image->resizeToWidth(300);
      $image->resizeToHeight(200);
      $image->save('resizeImage.jpg');
      //$image->output();
   } else {
?>   <form action="" method="post" enctype="multipart/form-data">
      <input type="file" name="uploaded_image" />
      <input type="submit" name="submit" value="Upload" />
   </form><?php
   }
?>

Check this link (dead) to find class of resize image and more detail.

Neri answered 10/3, 2011 at 6:0 Comment(0)
T
0

You may want to try imagemagick for this:

exec( "convert $newname -resize 58x58^ -gravity center -extent 58x58 /img_croped/$filename");

This will resize shorter side to 58, then crop longer side to 58 px (cutting off edges)

Thimblerig answered 10/3, 2011 at 4:28 Comment(2)
thanks for reply but the code is doesn't work with my function i did it, but the function doesn't upload to img_croped folderSwimmingly
@Swimmingly - I'm not sure if you have your /img_croped in the root like you have in your question, or somewhere else... The last parameter to the command line is where this file should be saved. Change it to the folder and file name you need, and it should work. If not - post what error you're getting.Thimblerig

© 2022 - 2024 — McMap. All rights reserved.