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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程