douan6931 2015-05-12 10:21
浏览 108
已采纳

图像调整大小和上传

I am trying to upload some images using import.csv file. The images need to be resized before uploading. I am using this code for uploading but it is returning a black image of the given size.

 <?php
error_reporting(E_ALL);
include('config.php');
include('inc/header.php');
define('CSV_PATH', '');
$csv_file = CSV_PATH . "importImage.csv"; // Name of your CSV file
$csvfile  = fopen($csv_file, 'r');
$theData  = fgets($csvfile);

$i = 0;
while (!feof($csvfile)) {

    $csv_data[] = fgets($csvfile, 1024);

    $csv_array  = explode(",", $csv_data[$i]);
    $insert_csv = array();
    $url        = $insert_csv['url'] = $csv_array[0];
    $image      = $insert_csv['image'] = $csv_array[1];

    $raw = file_get_contents($url . $image);


    /*RESIZE USING GD*/


    /*
     * PHP GD
     * resize an image using GD library
     */

    // File and new size
    //the original image has 800x600
    $filename = $raw;
    //the resize will be a percent of the original size
    $percent  = 0.5;

    // Content type
    header('Content-Type: image/jpeg');


    // Get new sizes
    list($width, $height) = getimagesize($filename);
    $newwidth  = 200;
    $newheight = 200;

    // Load
    $thumb  = imagecreate($newwidth, $newheight);
    $source = imagecreate($filename);

    // Resize
    $imgnew = imagecopyresampled($thumb, $source, 0, 0, 0, 0, newwidth, $newheight, $width, $height);

    imagejpeg($thumb, 'Mypath' . time() . '.jpg'); // save resized image to                                       

    //Output and free memory
    imagedestroy($thumb);
    $i++;
}

fclose($csvfile);

echo "File data successfully imported to database!!";
mysql_close($connect);

?>
  • 写回答

2条回答 默认 最新

  • dougong7850 2015-05-12 11:12
    关注

    The following script will easily allow you to resize images using PHP and the GD library.

    https://github.com/nomisoft/White-Hat-Classes/tree/master/Simple-Image

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛