【问题标题】:Edit and crop uploaded image编辑和裁剪上传的图像
【发布时间】:2011-07-12 10:37:35
【问题描述】:

你能帮我编辑这段代码吗?

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];
  }

我需要在这个函数中添加 RESIZE AND CROP IMAGE 像 flickr 和 facebook img 一样,我创建了一个新文件夹:/img_croped

我想在这个文件夹中插入新图片(resize andcrop 58*58px)

和:

 mysql_query("insert into images (small_img) VALUES('$croped')"); // URL VALUE

【问题讨论】:

  • 可能的图像类型有哪些?
  • 除了明显的 SQL 注入漏洞之外,不要使用 $_FILES 数组中的 sizenametype 值 - 它们是用户提供的并且可以被破坏。同样,不要使用copy。有一个move_uploaded_file 函数专门用于处理文件上传。

标签: php image resize crop uploader


【解决方案1】:

要调整图像大小和裁剪图像,请查看以下有助于裁剪图像的代码:

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

检查this link(已死)以查找调整大小图像的类别和更多细节。

【讨论】:

    【解决方案2】:

    您可能想为此尝试 imagemagick:

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

    这会将较短边的大小调整为 58,然后将较长边裁剪为 58 像素(切断边缘)

    【讨论】:

    • 感谢您的回复,但代码不适用于我的功能,我做到了,但该功能没有上传到 img_croped 文件夹
    • @rixlinux - 我不确定你的/img_croped 是否像你在问题中那样在根目录中,或者在其他地方......命令行的最后一个参数是这个文件的位置应该保存。将其更改为您需要的文件夹和文件名,它应该可以工作。如果没有 - 请发布您遇到的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2019-02-03
    • 1970-01-01
    • 2014-07-25
    • 2021-09-04
    • 2018-05-16
    相关资源
    最近更新 更多