调整远程图像的大小

时间:2011-09-13 07:48:40

标签: php html

<form id="form" name="form" method="post" action="process_image.php">
<textarea name="urls" cols="60" rows="10"></textarea>
<input type="submit" value="Upload Images" />
</form>

我应该在此代码中添加什么来自动调整图像大小,以宽度:175px和高度:自动

    <?PHP
 $urls = $_POST['urls'];
 $list = explode("\n", $urls);


 if (!file_exists("images")) {
  mkdir("images", 0755);
 }

 $files = array();

 foreach ($list as $links) {
  $image = get_image($links);
  $file = 'images/' . img_name($links);
  file_put_contents($file, $image);
  array_push($files, $file);
    }

 function get_image($url) {
   $ch = curl_init();
   $timeout = 5;
   curl_setopt($ch,CURLOPT_URL,$url);
   curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
   curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
   $data = curl_exec($ch);
   curl_close($ch);
   return $data;
 }

 function img_name($url) {
  $chars = array_merge(range(a, z), range(0, 9));
  $extension = get_extension($url);
  $name = "";
  do {
      for($i = 0; $i < 6; $i++) {
          $name .= $chars[rand(0, (count($chars) - 1))];
      }
  } while (file_exists('images/$name.$extension'));
  return $name . "." . $extension;
 }

 function get_extension($url) {
  $ext_pos  = strpos(strrev($url), ".");
  $ext = substr($url, (strlen($url) - $ext_pos), $ext_pos);
  return $ext;
 }

?>

在这里显示网址:

    <form name="form_process">
 <label>Images Uploaded</label>
 <textarea id="image_urls" cols="50" rows="10"><?PHP
  foreach($files as $uploaded) {
   echo "http://".$_SERVER['HTTP_HOST']. "/" .$uploaded . "\n";
  }
 ?></textarea>
    </form>

2 个答案:

答案 0 :(得分:0)

您真正需要的是调整图像大小。好吧,看看这个post如何调整图像大小。

答案 1 :(得分:0)

这可能是一个有用的开始,即使你决定自己推出:

Resizing images with PHP