Image resizing and loading

I am trying to load some images using import.csv file. Image needs to be changed before uploading. I am using this code to load, but it returns 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);

?>

      

+3


source to share


2 answers


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



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

0


source


Please make sure it $url.$image

is the real picture first.

Second, the getimagesize

image path is required, not the image



I'm sure you want this: http://runnable.com/UnF-tFdudNt1AABt/how-to-resize-an-image-using-gd-library-for-php

0


source







All Articles