I'm trying to resize a fullsize image poster into three smaller variants of the file. I first save the fullsize poster, then i use it's path to create the smaller ones. The problem is that the sizes are incorrect. I have a $conf array containing general config info for all images, and then i have a $conf array for each image with specific changes (eg. size). I clear the lib before every new init.
// Config for all images
$conf['source_image'] = $file_path;
$conf['quality'] = 80;
$conf['maintain_ratio'] = true;
$conf['master_dim'] = 'auto';
// Small
$conf['new_image'] = 'img/movie_images/posters_small/' . $file_name;
$conf['height'] = 75;
//$conf['width'] = 55;
$this->image_lib->initialize($conf);
$this->image_lib->resize();
$this->image_lib->clear();
//unset($conf['width']);
// Medium
$conf['new_image'] = 'img/movie_images/posters_medium/' . $file_name;
$conf['height'] = 200;
$this->image_lib->initialize($conf);
$this->image_lib->resize();
$this->image_lib->clear();
// Big
$conf['new_image'] = 'img/movie_images/posters_big/' . $file_name;
$conf['height'] = 300;
$this->image_lib->initialize($conf);
$this->image_lib->resize();
$this->image_lib->clear();
See any errors in the code? I tried outputting errors as someone advised in another question here, but that didn't show anything either. I don't understand how it works really. If i set "master_dim" to auto, the first (the small) image get's the right height, but the default width (which is really really wide). If i set master_dim to "width", the small image stays the same (really wide, but correct height). If i set master_dim to "height" they all get the correct height, but the small one still is super wide.
Any ideas?
As you can see in the code i tried playing around with the width of the small image to get it's correct size, but didn't get it to work.
After setting master_dim to height, and also setting the width of the small image manually, it works as i want it to.
You can download the image library from http://thephpcode.com/blog/codeigniter/gradient-image-library-for-codeigniter to convert the image to fit in to a correct width and size.
Related
Take a look at this script:
$path = 'storage/test-image.JPG';
$savePath = 'storage/test-image-saved.JPG';
$image = \Spatie\Image\Image::load(public_path($path));
$image
->height($image->getHeight()) // manipulating height to its original height
->save(public_path($savePath));
$originalSize = File::size(public_path($path)); // 1.7MB
$savedSize = File::size(public_path($savePath)); // 200KB
The result is that the image got compressed, and its colour changed. This is out of my expectation for an image manipulation tool.
Can anyone with experience care to explain it a bit?
Thank you.
I have a CI image upload form, and I want to make sure the images I upload will fit in a given rectangle. If they are too big - I want to downsize them. If they fit within that rectangle - I don't need resizing.
I know CI supports max-height and max-width (as a limitation on how big the uploaded images can be), and it supports resize, but I couldn't find how to resize the image to a set max-height and max-width (while maintining the ratio).
Well, it was easy enough, as expected from CI (-:
When using $this->image_lib->resize(), you set the desired width & height. If you also set maintain_ratio to true, the new image will be resized to the closest possible values of your set width & height, while preserving the original aspect ratio.
So this is the code I used, after uploading the image:
$file_data = $this->upload->data();
$max_height = 115;
$max_width = 225;
if ($file_data['image_width']>$max_width || $file_data['image_height']>$max_height)
{
$configResize = array(
'source_image' => $file_data['full_path'],
'width' => $max_width,
'height' => $max_height,
'maintain_ratio' => TRUE
);
$this->load->library('image_lib',$configResize);
$this->image_lib->resize())
}
You need to calculate the height and width you would like the resized image to be. For example:
Suppose your input image is 1200x1000 and you want your resized image to be 300x200.
Find the size ratios 1200/1000=1.2 and 300/200=1.5.
If the original ratio is larger than the targeted resized size ratio you want to match the width, else you want to math the height. In this case we want to match the height.
Find the scale factor to change the image to the target size. Scale factor is 200/1000=0.2.
Use the scale factor to find the new size 1200x1000 scaled by 0.2 (1200*0.2 and 1000*0.2) = 240x200.
Resize the image to 240x200 as it is your best fit for the 300x200 box.
Take from the website provided by Silviu G http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = 'originalImage.jpg';
$config['create_thumb'] = TRUE;
$config['width'] = 240;
$config['height'] = 200;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
Perhaps a better way to handle this is through CSS (in conjunction with the CI image manipulation library for resizing):
.media img{
max-width:100px;
max-height:100px;
}
Use the Image Manipulation Class form CI
I have an image that has been broken in to parts, 64 rows by 64 columns. Each image is 256x256px. The images are all PNG. They are named "Image--.png" for example "Image-3-57". The rows and columns numbering start from 0 rather than 1.
How can I assemble this back in to one image? Ideally using BASH and tools (I'm a sysadmin) though PHP would be acceptable as well.
Well, it is not very complicated, if you want to use PHP. What you need is just a few image gunctions - imagecreate and imagecopy. If your PNG is semi transparent, you will also need imagefilledrectangle to create a transparent background.
In code below, I rely on fact, that all chunks are same size - so the pixel size must be able to be divided by the number of chunks.
<?php
$width = 256*64; //height of the big image, pixels
$height = 256*64;
$chunks_X = 64; //Number of chunks
$chunks_Y = 64; //Same for Y
$chuk_size_X = $width/$chunks_X; //Compute size of one chunk, will be needed in copying
$chuk_size_Y = $height/$chunks_Y;
$big = imagecreate($width, $height); //Create the big one
for($y=0; $y<$chunks_Y; $y++) {
for($x=0; $x<chunks_X; $x++) {
$chunk = imagecreatefrompng("Image-$x-$y.png");
imagecopy($big, $chunk,
$x*$chuk_size_X, //position where to place little image
$y*$chuk_size_Y,
0, //where to copy from on little image
0,
$chuk_size_X, //size of the copyed area - whole little image here
$chuk_size_Y,
);
imagedestroy($chunk); //Don't forget to clear memory
}
}
?>
This is just a draft. I'm not sure about all theese xs and ys as well as ather details. It is late and I'm tired.
I am looking to make a photoshop action (maybe this isn't possible, any other application recommendations would be helpful as well). I want to take a collection of photos and make them a certain aspect ration, ex: 4:3.
So I have an image that is 150px wide by 200px high. What I would like to happen is the image's canvas is made to be 267px wide, with the new area filled with a certain color.
So there are two possibilities I can think of:
1) Photoshop actions could do this, but I would have to pull current height, multiply by 1.333333 and then put that value in the width box of the canvas resize. Is it possible to have calculated values in Photoshop actions?
2) Some other application has this feature built in.
Any help is greatly appreciated.
Wow, I see now (after writing the answer) that this was asked a long time ago. . . oh well. This script does the trick.
This Photoshop script will resize any image's canvas so that it has a 4:5 aspect ratio. You can change the aspect ratio applied by changing arWidth and arHeight. The fill color will be set to the current background color. You could create an action to open a file, apply this script, then close the file to do a batch process.
Shutdown Photoshop.
Copy this javascript into a new file named "Resize Canvas.jsx" in Photoshop's Presets\Scripts folder.
Start Photoshop and in the File - Scripts menu it should appear.
#target photoshop
main ();
function main ()
{
if (app.documents.length < 1)
{
alert ("No document open to resize.");
return;
}
// These can be changed to create images with different aspect ratios.
var arHeight = 4;
var arWidth = 5;
// Apply the resize to Photoshop's active (selected) document.
var doc = app.activeDocument;
// Get the image size in pixels.
var pixelWidth = new UnitValue (doc.width, doc.width.type);
var pixelHeight = new UnitValue (doc.height, doc.height.type);
pixelWidth.convert ('px');
pixelHeight.convert ('px');
// Determine the target aspect ratio and the current aspect ratio of the image.
var targetAr = arWidth / arHeight;
var sourceAr = pixelWidth / pixelHeight;
// Start by setting the current dimensions.
var resizedWidth = pixelWidth;
var resizedHeight = pixelHeight;
// The source image aspect ratio determines which dimension, if any, needs to be changed.
if (sourceAr < targetAr)
resizedWidth = (arWidth * pixelHeight) / arHeight;
else
resizedHeight = (arHeight * pixelWidth) / arWidth;
// Apply the change to the image.
doc.resizeCanvas (resizedWidth, resizedHeight, AnchorPosition.MIDDLECENTER);
}
Mind that the accepted answer from #user268911 may not work for you if the source image has different pixels/inch than 72. Because the UnitValue.convert function works correctly only with 72 px/inch. To be sure the conversion is correct for ever pixel/inch value, set baseUnit property as follows:
...
var pixelWidth = new UnitValue (doc.width, doc.width.type);
pixelWidth.baseUnit = UnitValue (doc.width.baseUnit, "in");
var pixelHeight = new UnitValue (doc.height, doc.height.type);
pixelHeight.baseUnit = UnitValue (doc.height.baseUnit, "in");
...
For more details about the conversion see "Converting pixel and percentage values" section of the Adobe JavaScript Tools Guide.
What languages do you know? ImageMagick has command line tools that can do this, but you'd need to know a scripting language to get the values and calculate the new ones.
For .NET, my company's product, DotImage Photo, is free and can do this (need to know C# or VB.NET)
I'm using Codeigniter's image manipulation library to re-size an uploaded image to three sizes, small, normal and large.
The re-sizing is working great. However, if I'm resizing a vertical image, the library is rotating the image so it's horizontal.
These are the config settings I have in place:
$this->resize_config['image_library'] = 'gd2';
$this->resize_config['source_image'] = $this->file_data['full_path'];
$this->resize_config['maintain_ratio'] = TRUE;
// These change based on the type (small, normal, large)
$this->resize_config['new_image'] = './uploads/large/'.$this->new_file_name.'.jpg';
$this->resize_config['width'] = 432;
$this->resize_config['height'] = 288;
I'm not setting the master_dim property because the default it set to auto, which is what I want.
My assumption is that the library would take a vertical image, see that the height is greater than the width and translate the height/width config appropriately so the image remains vertical.
What is happening (apparently) is that the library is rotating the image when it is vertical and sizing it per the configuration.
This is the code in place I have to do the actual re-sizing:
log_message('debug', 'attempting '.$size.' photo resize');
$this->CI->load->library('image_lib');
$this->CI->image_lib->initialize($this->resize_config);
if ($this->CI->image_lib->resize())
{
$return_value = TRUE;
log_message('debug', $size.' photo resize successful');
}
else
{
$this->errors[] = $this->CI->image_lib->display_errors();
log_message('debug', $size.' photo resize failed');
}
$this->CI->image_lib->clear();
return $return_value;
EDIT
I think the problem may be from the upload library. When I get the image_height and image_width back from the upload, the width seems to be larger even though I uploaded a vertical image.
This is a portion of the code I'm using to upload the photo:
$this->upload_config['allowed_types'] = 'jpg|jpeg';
$this->upload_config['max_size'] = '2000';
$this->upload_config['max_width'] = '0';
$this->upload_config['max_height'] = '0';
$this->upload_config['upload_path'] = './uploads/working/';
$this->CI->load->library('upload', $this->upload_config);
if ($this->CI->upload->do_upload($this->posted_file))
{
$this->file_data = $this->CI->upload->data();
$return_value = TRUE;
log_message('debug', 'upload successful');
}
I added some logging to check the values:
$this->is_vertical = $this->file_data['image_height'] > $this->file_data['image_width'];
log_message('debug', 'image height:'.$this->file_data['image_height']);
log_message('debug', 'image width:'.$this->file_data['image_width']);
if ($this->is_vertical)
{
$this->resize_config['master_dim'] = 'height';
}
else
{
$this->resize_config['master_dim'] = 'width';
}
log_message('debug', 'master_dim setting:'.$this->resize_config['master_dim']);
These are the results of the log:
DEBUG - 2010-03-16 18:35:06 --> image height:1536
DEBUG - 2010-03-16 18:35:06 --> image width:2048
DEBUG - 2010-03-16 18:35:06 --> master_dim setting:width
Looking at the image in photoshop, these are the dimensions:
height: 2048
width: 1536
Anyone know what might be causing the upload library to do this?
I've never used this library, but having read the documentation, I wonder whether the master_dim property might help. If you set this to 'height' for vertical images that might keep them the right way up. You could just parse each image through a conditional to see if the image is vertically aligned and then only set this property if need be.
My other thought is about the maintain_ratio property. The documentation says that with this set to 'TRUE' it will resize as close to the target values as possible whilst maintaining the aspect ratio. I wonder if it thinks that rotating the image allows it to preserve this ratio more accurately? As an experiment, try setting this value to 'FALSE' for vertical images.
Ok - I decided not to trust photoshop and opened the images I was testing in quicktime and safari. I discovered that they were actually still horizontal.
So Codeigniter was operating exactly as expected.
I went back into photoshop, did a save for web on the test images, re-uploaded them and it worked as expected.
I then stripped out the extra code that I had added to test whether the image was vertical and the library works as I expected it would.
Now - I need to figure out how to prevent end users from doing this exact thing.
Thanks for taking the time to answer my question musoNic80. Hopefully someone else can learn from my mistakes here.