Hey :) I want to resize an image when I address it in a form(save to database). I dont think it will be a problem [pointing to the image etc, however the space I am going display the thumb in is 228 x 228. I know I can maintain the ratio and determine the size in the controller, yet how is it done on images that are frequently different sizes?
Is the best way to address this is to put pre-calculated values(make my own ratio) in my form so that the controller can use those values to determine max height and width? See code.
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75; // use $this->input->post('width') instead??
$config['height'] = 50; // use $this->input->post('height')
Or is there a way to tell the function no bigger than 228 and no wider than 228?
Thanks
I am not sure what you want exactly, but by using the following settings you can make sure either the width or the height of the image is 228px. If width > height, width will be 228px. And vice versa.
$config['maintain_ratio'] = TRUE;
$config['master_dim'] = 'auto' // auto is default, so you can leave this out
$config['width'] = 228;
$config['height'] = 228;
master_dim
Specifies what to use as the master
axis when resizing or creating thumbs.
For example, let's say you want to
resize an image to 100 X 75 pixels. If
the source image size does not allow
perfect resizing to those dimensions,
this setting determines which axis
should be used as the hard value.
"auto" sets the axis automatically
based on whether the image is taller
then wider, or vice versa.
If this is not what you mean, please add a bit more explanation.
getImageSize()
I think you'd have to throw some logic in your controller to handle this. Using the function above you can test the height/width of the uploaded image and resize if necessary.
Related
I maintain a site that has a database of roughly 30,000 images associated with a listing. The images are many different sizes and aspect ratios, so in order to present them in a uniform manner on the site I've written a function that allows me to present the image responsively at a specific aspect ratio.
function image($src,$aspect) {
$html = '<img class="img-responsive" src="furniture/blank-'.$aspect.'.png" style="background:url(\''.$src.'\');background-position:center center;background-size:cover;" >';
return $html;
}
In my 'furniture' folder I have a set of transparent PNG files in the desired aspect ratio to force the size of the container that the actual image becomes the background to. So, there's a 4x3.png file for instance, which is just a 4px wide by 3px high empty graphic that I can call with image('path/to/image.jpg','4x3') - it works pretty well.
However, the problem comes when the image has someone's face in it, as the default 'center center' positioning can sometimes cut off heads.
I've already done the step of face detection, and each record in the database has a flag as to whether a face was found in the image and if so, the X, Y and Width values. The issue is then trying to figure out the background position because I'm dealing with a responsive layout, so don't know necessarily know what size the image is displayed.
I'm assuming the logic I'll need to follow is;
IF image has face in it
THEN set the background-position with a vertical offset calculated based on the Y coordinate of the face
ELSE
set the background-position to center center
How do I figure out the value of the vertical offset bearing in mind the size of the image is unknown?
Ok, so figured this one out but posting here just in case anyone else is struggling with it.
The basic logic is to work out the height of the aspect window based on the width of the actual image (no need to worry about the responsive nature at this stage). You then find the ideal central point of the image to put in the frame and work out whether that point is above or below the limits of how your image can slide within your aspect window.
I put this into a function I call and then drop the result directly into the background-position CSS.
In the below, the variables are:
$img = path to the image
$aspect1 = e.g. 16 (if 16x9)
$aspect2 = e.g. 9 (if 16x9)
$faceD = Set to 1 if the image has a face detected in it
$faceY = Y pixel of top of face
$faceW = Width (and Height) of face area
function calcImagePosition($img,$aspect1,$aspect2,$faceD,$faceY,$faceW) {
if($faceD==1) { // Only do this if there's a face in the image
list($width, $height) = getimagesize($_SERVER['DOCUMENT_ROOT']."/".$img); // Get the image dimensions
$aspectHeight = ($width/$aspect1)*$aspect2; // Get the height of the aspect window based on the actual width of the image
$idealCentre = $faceY + ($faceW/2); // The best part of the image to appear in the aspect window is halfway down the face bounds
if($idealCentre<($aspectHeight/2)) { // if the ideal centre is higher than half the height of the aspect window, just set it to top
$position = "center top";
} elseif(($height-$idealCentre)<($aspectHeight/2)) { // Also, if there's less than half the height of the aspect window at the bottom, set it to bottom
$position = "center bottom";
} else { // This will slide the position to best match the face
$pixel = $idealCentre-($aspectHeight/2); // Get the absolute pixel value
$percent = intval(($pixel/$height)*100); // Convert this to a percentage so it doesn't matter about responsive sizing
$position = "center ".$percent."%";
}
} else {
$position = "center center"; // default for no face detection
}
return $position;
}
I'm using this in conjunction with Karthik Tharavaad's php port of the Face Detector which does a reasonable job of creating the data needed for $faceY and $faceW.
I want to resize an image with custom ratio (width:height)=(5:1)
Using Intervention image manipulation library in laravel.
It's not a problem if the image stretches. I don't want to put any fixed height or width.
so please give me some suggestions.
I think the best solution might be, to use fit() from the library.
Like this:
// open 4/3 image for example
$image = Image::make('foo.jpg');
// your desired ratio
$ratio = 16/9;
// resize
$image->fit($image->width(), intval($image->width() / $ratio));
It don't stretches the image.
I don't think intervention image library has this option in their resize function. you can use getimagesize() php function to get the height and width and divide width with 5 (in your case its 5 because you want 5:1) to get the height.
$image=getimagesize($image_file);
$width=$image[0]; // $image[0] is the width
$height=$image[0]/5; // $image[1] is the height
Than you can just use your intervention's resize() function to resize to that height and width.
Image::make($source_image)
->resize($width,$height ,false,false)
->save($destination);`
I choose fit() rather than resize() to modify the picture avoiding to stretch the image to much.
I use a php snippet in my project, which might be helpful.
$img = Image::make($pictureOriginalPath);
// Picture ratio
$ratio = 4/3;
// Check the current size of img is appropriate or not,
// if ratio of current img is greater than 1.33, then crop
if(intval($img->width()/$ratio > $img->height()))
{
// Fit the img to ratio of 4:3, based on the height
$img->fit(intval($img->height() * $ratio),$img->height());
}
else
{
// Fit the img to ratio of 4:3, based on the width
$img->fit($img->width(), intval($img->width()/$ratio));
}
// Save, still need throw exception
$img->save($pictureNewPath);
When I am creating thumbnail in codeigniter I get an image with black background color.
Why did it happen?
This is the code in controller:
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = $file ;
$config['maintain_ratio'] = FALSE;
$this->image_lib->initialize($config);
$this->md_image->resize_image('./images/logo_images/'.$filename,380,80,'./images/logo_images/thumbs/'.$filename);
It is a PHP and GD2 problem, and not taken into account in the Image Lib in CodeIgniter.
Have a look here in the PHP manual to be able to keep transparency on resize :
http://www.php.net/manual/en/function.imagecolortransparent.php
Stéph
UPDATE: THE ANSWER IS USE THE GD2 LIBRARY, DOH!
I am working with Codeigniter's image manipulation library to resize some photos. Unfortunately, they are producing a blue tint or hue to the photos. Not sure why this is and needed to see if it was something I am doing. Here is the code I am using to create the thumb's. Let me know if you want to see image links, and I will upload them somewhere.
$this->load->library('image_lib');
$config['image_library'] = 'GD';
$config['source_image'] ="images/IMG_0007.jpg";
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = '450';
$config['height'] = '450';
$this->image_lib->initialize($config);
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
I strongly recommend you to use ImageMagick for image resizing. It respects color profiles, is slightly faster, less memory hungry and generally produces better quality. See this question:
How to stop GD2 from washing away the colors upon resizing images?
If you do not have ImageMagick installed, this may be of help:
http://ferdychristant.com/blog//archive/DOMM-8GAFGL
I have uploaded an image with the resolution 1600x1200. How can I show users 60x60 and 200x200 without creating thumbnails and uploading again. (like phpthumb library) in CodeIgniter?
http://codeigniter.com/forums/viewthread/155086/
That's the timThumb controller for CI.
Codeigniter comes with its own image library which can resize images. This example is from the documentation, just edit the $config variable:
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['new_image'] = '/path/to/new/image.jpg';
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
You can also create thumbnails dynamically (that is, without saving them to the hard drive) but that requires a lot of memory so I would discourage that.
The image class can also crop the image if you need that. I use it to create thumbnails in the application I am currently working on...
Visit the documentation: http://codeigniter.com/user_guide/libraries/image_lib.html