php - 图像调整大小和上传

标签 php image-resizing

我正在尝试使用 import.csv 文件上传一些图像。图片需要在上传前调整大小。我正在使用此代码进行上传,但它返回给定大小的黑色图像。

 <?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);

?>

最佳答案

以下脚本将允许您使用 PHP 和 GD 库轻松调整图像大小。

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

关于php - 图像调整大小和上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30188006/

相关文章:

php - 将数组的 var_dump 转换回数组变量

html - 根据屏幕自动调整图像

flutter - 创建可调整大小的 View ,当在 FLUTTER 中从角落和侧面捏或拖动时调整大小

java - 如何使用 Java 调整图像大小?

c# - DrawImage 调整后的图像太小

c - 如何正确调整位图的大小?

javascript - 将表单数据收集到 Google 表格时遇到更多麻烦

php - 此后如何使用 explode ,因为在我的数据库列中,所有值都在产品列中可用,如下所示

php - 如何在php中计算总价

php - 获取下个月的第一天