I need to resize the uploaded image and save it with given resolutions. Assume user uploads just only one single image and I save it like 35x35, 100x100 and 512x512 after finishing the upload. finally his one upload save in my folder as 3 images with different resolutions. I've done up to this point using laravel...
public function postSingleUpload()
{
//create the relevant directory to add the user image
//get the directory name (directory name equals to user id)
$dirPath = sprintf("images/users/avatar/%s/", Auth::user()->id);
//create the directory named by user id
if (!file_exists($dirPath)) {
mkdir($dirPath, 0700);
}
$file = Input::file('image');
//save image with given resulutions
//---- this part i need --------//
}
so please help me for this.
Here is what I have done to save the uploaded image with given resolutions:
//First Copy the uploaded image to some location
Input::file('profilePic')->move('Users/'.$username.'/Wallpics/',$name)
//Set this attribute for quality after resampling
$quality = 90;
$src = 'Users/'.$username.'/Wallpics/'.$name;
//Run this on recently saved uploaded image
$img = imagecreatefromjpeg($src);
//get this values from user by submitting form ( either by crop or by textboxes)
$width=(int)Input::get('w');
$height=(int)Input::get('h');
$x=(int)Input::get('x');
$y=(int)Input::get('y');
//This is the code to resample the image and generate a new to ur requirements
$dest = ImageCreateTrueColor($width, $height);
imagecopyresampled($dest, $img, 0, 0,$x,$y, $width, $height,$width,$height);
imagejpeg($dest, 'Users/'.$username.'/profilePic/'.$name, $quality);
//Set the path in database
$profile->profilePic=asset('Users/'.$username.'/profilePic/'.$name);
$profile->save();
Related
I'm trying to create thumbnail of image while it is uploading. The problem is that the thumbnail isn't created at all. Also is not saved in database.
This is what I have added in my function
$image = $request->file('image');
if( $image && $image->isValid()){
$imagename = str_random(20).'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/uploads');
$thumb_img = Image::make($image->getRealPath())->resize(100, 100);
$thumb_img->save($destinationPath.'/'.$imagename,80);
$image->move($destinationPath, $imagename);
}
$item->image = $filename;
$item->image_thumb = $thumb_img;
It's saves only the original image both places - uploads dir and in database but nothing regarding the thumbnail.
I'm using Intervention package.
Nothing is saving because you override the same image twice. Look what you have:
First, you creating thumbnail and saves it into the /uploads
After this, you save the original into the same directory with same name e.g. overriding the thumb.
You just need to make different name for the thumbnail:
$thumb_img = Image::make($image)->resize(100, 100)->save($destinationPath.'/thumb_'.$imagename, 80);
Notice the prefix for the thumb thumb_...
I am currently creating a component, where a user creates a new image and there's an upload button and a name input, all the basic gallery fields for an Image..
But I have come across two issues, the one is how would i write the following code so that it exports one image b/w and the other color scaled to amax width of 160, Here's the code I have:
function save(){
if(!defined('DS')) define('DS', DIRECTORY_SEPARATOR);
$input=JFactory::getApplication()->input;
$input->get('jform', NULL, NULL);
$file = JRequest::getVar('jform', null, 'files', 'array');
$data = JRequest::getVar( 'jform', null, 'post', 'array' );
$path = JPATH_ROOT;
//
//
// Make the file name safe.
jimport('joomla.filesystem.file');
$file['name']['logo'] = JFile::makeSafe($file['name']['logo']);
// Move the uploaded file into a permanent location.
if (isset($file['name']['logo'])) {
// Make sure that the full file path is safe.
$filepath = JPath::clean($path. DS ."images". DS ."associations". DS . strtolower($file['name']['logo']));
// Move the uploaded file.
JFile::upload( $file['tmp_name']['logo'], $filepath );
$data['logo'] = strtolower( $file['name']['logo'] );
//convert image
$image = $filepath;
$im = new Imagick();
$im->pingImage($image);
$im->readImage( $image );
$im->setImageResolution(72,72);
$im->resampleImage(72,72,imagick::FILTER_UNDEFINED,1);
$im->scaleImage(160,0);
$im->setImageFormat('jpeg');
$im->setImageCompression(imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality(60);
$im->modulateImage(100, 0, 100);
$im->writeImage($image);
$im->destroy();
}
$input->post->set('jform',$data);
return parent::save();
}
And Finally, How Would I manage this? it saves the image name in the database but once i return to this item it just has an upload field, where it would be rather useful to have the image name or the actual image with a delete function showing and a reupload...
Am I going the right way about this?... Any Help Greatly Appreciated... Thank you :)
How to produce 2 images
You have to create 2 resources out from the uploaded original, transform each and save in the separate file. For example I'be using the JImage package (note there's no limitcolors filter available)
$image_one = new JImage($filepath);
$image_one
->filter('grayscale')
->toFile($path_one, IMAGETYPE_JPEG, array('quality' => 60));
$image_two = new JImage($filePath);
$image_two
->filter('limitcolors', array('limit' => 160))
->toFile($path_two, IMAGETYPE_GIF);
Managing images
Depends on the reason, why there"s functionality being implemented and so relation of the uploaded image to other objects/ events.
If the uploaded images are used for user profiles, you should store a user id in the database along with image location. Next time check if current users has any images related to his/ her account and display.
thanks for taking the time to read this. I have a form with 35 file input boxes as part of a CMS for my customer to upload 35 pictures of each his products. The breakdown of that is 7 pictures of the black version, 7 pictures of the blue, 7 pictures of the grey, 7 pictures of the red, and 7 pictures of the white version of each product. So that's 35 total pictures he needs to upload. Additionally, for each of those files that he uploads, a smaller "thumbnail" sized picture needs to be made. I have a file upload script that I always use that works beautifully - when there's one file to upload. I'm not sure how to apply it in this case for 35 files. Each input box has a unique name (black1, black2...black7, blue1, blue2...blue7, etc) so, technically I could repeat the upload code 35 times with the unique name of each file input box to do this, but that is obvoiusly extremely inefficient. I'm hoping someone here can help me out with a better solution.
An additional requirement is that the names of the files be stored in a database. All of the filenames of the black pictures should be put into a string, separated by commas, and stored in the blackpics column of the database. All of the filenames of the blue pictures should be put into a string, separated by commas, and stored in the bluepics columns of the database. And so on for the grey, red, and white pictures.
Here is the code that I have now to upload one file. It gets the file from input box "file", checks that it's of the right extension (an image file), checks the filesize, creates a random file name with a random number and timestamp, creates a thumbnail (448px x 298px - big thumbnail, I know), checks that the original image uploaded was of the right dimensions (873px x 581px), and if everything is okay, I end up with the big file saved in ../images/store/big/ and the thumb saved in ../images/store/small/. They both have the same filename, they're just stored in different directories. Temporary files are deleted and all that, and if there are any errors, the files are deleted. As I said, this works great for one file.
So what I need to do is modify the code so that it does all of this for input box "black1", "black2"..."black7", then saves all the filenames into a string (black1.jpg,black2.jpg,black3.jpg,black4.jpg,black5.jpg,black6.jpg,black7.jpg) which I can then store in the 'blackpics' column of the database. Same for the blue, grey, red, and white. I don't need any help with the database part. I'm thinking that I need to create a function containing the file upload script that returns the filename. Then call that function 35 times, one for each of the input boxes. But I could be wrong.
If anyone could offer me any assistance, I would greatly appreciate it. Here is the code for the upload script:
<?php
$filename = $_FILES["file"]["name"];
$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
$file_ext = substr($filename, strripos($filename, '.')); // get file name
$filesize = $_FILES["file"]["size"];
$allowed_file_types = array('.jpg','.gif','.png', '.JPG');
if (in_array($file_ext,$allowed_file_types) && ($filesize < 1024000)) {
// rename file
$rand = rand(1,100000000);
$time = time();
$newfilename = $rand . $time . $file_ext;
if (file_exists("../images/store/big/" . $newfilename)) {
// file already exists error
$err[] = "You have already uploaded this file.";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "../images/store/big/" . $newfilename);
$pathToImage = '../images/store/big/' . $newfilename;
$pathToThumb = '../images/store/small/' . $newfilename;
$last4 = substr($pathToImage, -4);
switch(strtolower($last4)) {
case '.jpeg':
$img = imagecreatefromjpeg($pathToImage);
break;
case '.jpg':
$img = imagecreatefromjpeg($pathToImage);
break;
case '.png':
$img = imagecreatefrompng($pathToImage);
break;
case '.gif':
$img = imagecreatefromgif($pathToImage);
break;
default:
exit('Unsupported type: '. $pathToImage);
}
$max_width = 448;
$max_height = 298;
// Get current dimensions
$old_width = imagesx($img);
$old_height = imagesy($img);
// Calculate the scaling we need to do to fit the image inside our frame
$scale = min($max_width/$old_width, $max_height/$old_height);
// Get the new dimensions
$new_width = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);
$tmp_img = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
switch(strtolower($last4)) {
case '.jpeg':
imagejpeg($tmp_img, $pathToThumb);
break;
case '.jpg':
imagejpeg($tmp_img, $pathToThumb);
break;
case '.png':
imagepng($tmp_img, $pathToThumb);
break;
case '.gif':
imagegif($tmp_img, $pathToThumb);
break;
default:
exit('Unsupported type: '. $pathToImage);
}
imagedestroy($tmp_img);
imagedestroy($img);
}
} elseif (empty($file_basename)) {
$err[] = "Select a file to upload";
} elseif ($filesize > 1024000) {
$err[] = "File size limit exceeded";
} else {
$err[] = "File type not allowed";
unlink($_FILES["file"]["tmp_name"]);
}
list($width, $height) = getimagesize("../images/store/big/$newfilename");
if ($width != "873" || $height != "581") {
$err[] = "File dimensions error";
unlink("../images/store/big/$newfilename");
unlink("../images/store/small/$newfilename");
}
?>
And in the body I have the file upload fields as so...
<input type="file" name="black1" disabled="1">
<input type="file" name="black2" disabled="1">
...
<input type="file" name="black7" disabled="1">
<input type="file" name="blue1" disabled="1">
<input type="file" name="blue2" disabled="1">
...
<input type="file" name="blue7" disabled="1">
and so on for grey, red, and white.
Like I said, if anyone can help me out, I would greatly appreciate it. And if you made it all the way down here, thanks again for taking the time to read all of this.
First don't use dimensions for images. Dimensions do not say much about the size of the image. And the size matters for displaying the image on a website, not the dimensions.
Second why not use a multipart uploading form? See here. And then your client could select the images colourwise and upload them with one selection, which would reduce the clicks from 35 to seven. Or if you trust your client to be more tech-savvy: Use only one input field and instruct him to name his files in a specific way. Like "b_[name of file].[extension]" for a black image. Then use your favourite string searching method - for example RegEx - to identify the images classes.
I am using a form to upload an image. Upon image upload, we will be able to see the uploaded image. Then I used JCrop (http://deepliquid.com/content/Jcrop.html) to allow cropping for this image. Let's assume I only care about JPEG images. I am then ready to submit the form.
Upon form submission I will perform some image manipulation and crop the image. However I want to put the information for this cropped image back into the $_FILES array (this is a must). How would I go about manipulating this $_FILES array in a PHP script?
Here is what I had attempted and it would not work.
$upload_dir = '/Users/user/Sites/tmp/';
$file_name = $_FILES['image']['name'];
$file_name = "cropped_".$file_name;
$tmp_name = $_FILES['image']['tmp_name'];
$file_size = $_FILES['image']['size'];
$src_file = imagecreatefromjpeg($tmp_name);
list($width,$height) = getimagesize($tmp_name);
// Creates cropped image
$tmp = imagecreatetruecolor($_POST['w'], $_POST['h']);
imagecopyresampled($tmp, $src_file, 0, 0, $_POST['x'], $_POST['y'], $_POST['w'], $_POST['h'], $_POST['w'], $_POST['h']);
$small_pic_file_path = $upload_dir.$file_name;
imagejpeg($tmp,$small_pic_file_path,85);
$message = "<img src='http://localhost/~user/tmp/".$file_name."'>";
$_FILES['image']['name'] = $file_name;
$_FILES['image']['type'] = "image/jpeg";
// unlink($tmp_name);
// if(!move_uploaded_file($upload_dir.$file_name, $tmp_name)) echo "Failure Moving Image";
$_FILES['image']['tmp_name'] = $upload_dir.$file_name;
$_FILES['image']['error'] = 0;
$sizes = getimagesize($upload_dir.$file_name);
$_FILES['image']['size'] = ($sizes['0'] * $sizes['1']);
It is allowable to change this $_FILES array?
Only changing in the global array won't work. You are just saying $_FILES['image']['tmp_name'] will be the newly created one. But the tmp location is having same old file.
But you have to update the image in /tmp folder.
Get your tmp path and copy the image there.
kind of copy($newImage, /tmp) - get your tmp folder path from php.ini
It's not a good idea to change a superglobal you'd better copy it into another variable and after do whatever you want.
I am trying to upload various images into a dynamically created folder on my server, then take each image and resize it while uploading it into the folder as well creating a new image and a new name.. example: image.jpg (original image) and image-resized.jpg (being the thumbnail image).
The thing I can not figure out is how to resize all images. I am not sure if I should put it in a loop. All I need is for each image I upload (could be 5 a time). It loops through and resizes them all and not just a single image. Here is my code any help would be appreciated!
Code to create folders and move picture into those folders:
// Desired folder structure
$structure = './Fotos/'.$newfolder;
// To create the nested structure, the $recursive parameter
// to mkdir() must be specified.
if (!mkdir($structure, 0, true)) {
die('Failed to create folders...');
}else{
$placefoldername = mysql_query("INSERT INTO datefolders (FolderDate) VALUES ('$newfolder')") or die(mysql_error());
echo "<div class=\"success\">El folder fue agregado con exito.<input type=\"button\" name=\"close\" value=\"X\" class=\"close\" /></div>";
}}
// ...
}
if(isset($_POST['upload'])){
$FolderDate = $_POST['fecha-folder'];
$FolderName = $_POST['FolderName'];
$hour = $_POST['hour'];
// Desired folder structure
$structure = './Fotos/'.$FolderDate.'/'.$hour.'/'.$FolderName;
// To create the nested structure, the $recursive parameter
// to mkdir() must be specified.
for($i=0;$i<count($_FILES['fileupload']['name']);$i++) {
$names = $_FILES['fileupload']['name'][$i];
$target_path = "Fotos/".$FolderDate."/".$hour."/".$FolderName."/";
$target_path = $target_path . basename( $_FILES['fileupload']['name'][$i]);
if(move_uploaded_file($_FILES['fileupload']['tmp_name'][$i], $target_path)) {
$success = 1;
Code to create a smaller (resized image) and also place into the already created folder:
$img = $names;
$imgPath = $structure;
function resizeImage($img, $imgPath, $suffix, $by, $quality)
{
//Create a thunbnail image by resizing the picture
// Open the original image.
$original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original (<em>$imgPath/$img</em>)");
list($width, $height, $type, $attr) = getimagesize("$imgPath/$img");
// Determine new width and height.
$newWidth = ($width/$by);
$newHeight = ($height/$by);
// Resample the image.
$tempImg = imagecreatetruecolor($newWidth, $newHeight) or die("Cant create temp image");
imagecopyresized($tempImg, $original, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) or die("Cant resize copy");
// Create the new file name.
$newNameE = explode(".", $img);
$newName = ''. $newNameE[0] .''. $suffix .'.'. $newNameE[1] .'';
// Save the image.
imagejpeg($tempImg, "$imgPath/$newName", $quality) or die("Cant save image");
// Clean up.
imagedestroy($original);
imagedestroy($tempImg);
return true;
}
$resize = resizeImage($img, $imgPath, "-resized", 23, 100);
Why are you defining the function resizeImage in the for loop? It is being redefined every time the loop iterates. This could be part of the problem. Define the function outside the loop and see if that works.
you can try;
when your first image proccess end use imagedestroy() then second image will proccessed.