【问题标题】:Image Resize and Upload图片调整大小和上传
【发布时间】:2015-05-12 10:21:50
【问题描述】:

我正在尝试使用 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 image-resizing


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      首先,检查$url.$image 是否为真实图像。

      其次,getimagesize需要图片路径,而不是图片

      我很确定你想要这个:http://runnable.com/UnF-tFdudNt1AABt/how-to-resize-an-image-using-gd-library-for-php

      【讨论】:

        猜你喜欢
        • 2011-08-05
        • 2011-10-08
        • 1970-01-01
        • 2011-08-25
        • 2014-08-26
        • 2011-05-28
        • 2016-10-11
        相关资源
        最近更新 更多