Jump to content

Recommended Posts

So basically, I am using a script that automatically grabs an image and create a thumbnail. It works like a charm on a different enviroment where only 1 image is uploaded, but when I try to upload multiple files, I get several errors.

 

 

foreach ($_FILES["pictures"]["name"] as $key => $value) {

$uploaddir = 'albums/'.$album.'/original/'; //a directory inside
$uploadfile = $uploaddir . basename($_FILES["pictures"]["name"][$key]);

if (move_uploaded_file($_FILES["pictures"]["tmp_name"][$key], $uploadfile)) { echo $value . ' uploaded<br>'; }

include 'cropimage.php';


$crop = new Crop_Image_To_Square;
$crop->source_image = $_FILES['pictures']['tmp_name'][$key];

$ext = end(explode('.', $_FILES['pictures']['name'][$key]));

$crop->save_to_folder = 'albums/'.$album;  
$crop->new_image_name = basename($_FILES["pictures"]["name"][$key]);

$process = $crop->crop('center');

 

 

cropimage.php:

 

<?php
/*
--------------------------------------------------------------------------------------------
Credits: Bit Repository 

Source URL: http://www.bitrepository.com/web-programming/php/crop-rectangle-to-square.html
--------------------------------------------------------------------------------------------
*/ 

/* Crop Image Class */

class Crop_Image_To_Square {

var $source_image;
var $new_image_name;
var $save_to_folder;

function crop($location = 'center')
{
$info = GetImageSize($this->source_image);

$width = $info[0];
$height = $info[1];
$mime = $info['mime'];

// What sort of image?

$type = substr(strrchr($mime, '/'), 1);

switch ($type)
{
case 'jpeg':
    $image_create_func = 'ImageCreateFromJPEG';
    $image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
    break;


case 'png':
    $image_create_func = 'ImageCreateFromPNG';
    $image_save_func = 'ImagePNG';
$new_image_ext = 'png';
    break;

case 'bmp':
    $image_create_func = 'ImageCreateFromBMP';
    $image_save_func = 'ImageBMP';
$new_image_ext = 'bmp';
    break;

case 'gif':
    $image_create_func = 'ImageCreateFromGIF';
    $image_save_func = 'ImageGIF';
$new_image_ext = 'gif';
    break;

case 'vnd.wap.wbmp':
    $image_create_func = 'ImageCreateFromWBMP';
    $image_save_func = 'ImageWBMP';
$new_image_ext = 'bmp';
    break;

case 'xbm':
    $image_create_func = 'ImageCreateFromXBM';
    $image_save_func = 'ImageXBM';
$new_image_ext = 'xbm';
    break;

default:
$image_create_func = 'ImageCreateFromJPEG';
    $image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
}

// Coordinates calculator

   if($width > $height) // Horizontal Rectangle?
   {
   if($location == 'center')
       {
       $x_pos = ($width - $height) / 2;
       $x_pos = ceil($x_pos);

       $y_pos = 0;
   }
   else if($location == 'left')
   {
   $x_pos = 0;
   $y_pos = 0;
   }
   else if($location == 'right')
   {
   $x_pos = ($width - $height);
   $y_pos = 0;
   }

       $new_width = $height;
       $new_height = $height;
   }
   else if($height > $width) // Vertical Rectangle?
   {
   if($location == 'center')
       {
       $x_pos = 0;

       $y_pos = ($height - $width) / 2;
       $y_pos = ceil($y_pos);
       }
   else if($location == 'left')
   {
   $x_pos = 0;
   $y_pos = 0;
   }
   else if($location == 'right')
   {
   $x_pos = 0;
   $y_pos = ($height - $width);
   }

       $new_width = $width;
       $new_height = $width;

   }

$image = $image_create_func($this->source_image);

$new_image = ImageCreateTrueColor($new_width, $new_height);

// Crop to Square using the given dimensions
ImageCopy($new_image, $image, 0, 0, $x_pos, $y_pos, $width, $height);

if($this->save_to_folder)
	{
       if($this->new_image_name)
       {
       $new_name = $this->new_image_name.'.'.$new_image_ext;
       }
       else
       {
       $new_name = $this->new_image_name(basename($this->source_image)).'_square_'.$location.'.'.$new_image_ext;
       }

	$save_path = $this->save_to_folder.$new_name;
	}
	else
	{
	/* Show the image (on the fly) without saving it to a folder */
	   header("Content-Type: ".$mime);

       $image_save_func($new_image);

	   $save_path = '';
	}

// Save image 

$process = $image_save_func($new_image, $save_path) or die("There was a problem in saving the new file.");

return array('result' => $process, 'new_file_path' => $save_path);
}
}

function new_image_name($filename)
{
$string = trim($filename);
$string = strtolower($string);
$string = trim(ereg_replace("[^ A-Za-z0-9_]", " ", $string));
$string = ereg_replace("[ \t\n\r]+", "_", $string);

$string = str_replace(" ", '_', $string);
$string = ereg_replace("[ _]+", "_", $string);

return $string;
}

?>

 

 

 

 

Warning: getimagesize(Array) [function.getimagesize]: failed to open stream: No such file or directory in /home/q94/public_html/alumnify.com/cropimage.php on line 20

 

Warning: imagecreatefromjpeg(Array) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /home/q94/public_html/alumnify.com/cropimage.php on line 130

 

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/q94/public_html/alumnify.com/cropimage.php on line 132

 

Warning: imagecopy(): supplied argument is not a valid Image resource in /home/q94/public_html/alumnify.com/cropimage.php on line 135

 

Fatal error: Call to undefined method Crop_Image_To_Square::new_image_name() in /home/q94/public_html/alumnify.com/cropimage.php on line 145

 

 

Any ideas? Any other ways of creating thumbnails for multiple images?

 

Thanks

$uploaddir = 'albums/'.$album.'/original/'; //a directory inside
$uploadfile = $uploaddir . basename($_FILES["pictures"]["name"][$key]);

if (move_uploaded_file($_FILES["pictures"]["tmp_name"][$key], $uploadfile)) { echo $value . ' uploaded<br>'; }

include 'cropimage.php';


$crop = new Crop_Image_To_Square;
$crop->source_image = $_FILES['pictures']['tmp_name'][$key];

 

Isnt that moving the file to a different location then requesting the file from the location from which it was moved?

 

Also, what permissions are the files set to and does it require extra permissions?

 

Warning: getimagesize(Array) [function.getimagesize]: failed to open stream: No such file or directory in /home/q94/public_html/alumnify.com/cropimage.php on line 20

 

This is where it all starts. It means i cannot find the file you are trying to upload.

do a vardump or print_r on $_FILES["pictures"] to see if it are multiple images you are sending.

print_r($_FILES["pictures"])

 

And what does your upload form look like ?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.