I had used gd2 library to resize image.But some images got rotated by 90 degree after resized but not all image.I hadn't used any rotation.What may be the reason behind this?Do I need to set anything other configuration?
My code is here :
if(isset($_FILES) && !empty($_FILES)){
$item_images = $_FILES;
$this->load->library('upload');
$this->load->library('image_lib');
$config['upload_path'] = './temp/';
$config['allowed_types'] = '*';
$this->upload->initialize($config);
//print_r($item_images);
foreach($item_images as $ind=>$img_name){
if($this->upload->do_upload($ind)){
$data_image = $this->upload->data();
//for cropping
$config_crop['image_library'] = 'gd2';
$config_crop['source_image'] = $config['upload_path'] . $data_image['file_name'];
$config_crop['create_thumb'] = FALSE;
$config_crop['maintain_ratio'] = TRUE;
$config_crop['width'] = 300;
$config_crop['height'] = 300;
//$config_crop['rotation_angle'] = '0';
//$config_crop['x_axis'] = '100';
//$config_crop['y_axis'] = '40';
$this->image_lib->initialize($config_crop);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
}
$image_thumb = $this->s3_model->upload_image('thumb_'.$img_name['name'] ,$config['upload_path'] . $data_image['file_name']);
if($image_thumb){
$result = $this->s3_model->upload_image($img_name['name'] ,$img_name['tmp_name']);
//unlink($config['upload_path'] . $data_image['file_name']);
}
if(! $result){
$flag = false; //Error in uploading image.
break;
}
}
}
Do you have a OSx working machine?
In my case, a pic, that in my Mac was shown correctly, indeed was turned sideways, that is because OSX preview detects CellPhone orientation while the pic was taken....
The server not, neither windows machines... so that was driving me crazy...
Being or not this your case, you can use exif_read_data() function. This will give you orientation data if it exists on photo's metadata.
Check here: How to detect shot angle of photo, and auto rotate for website display like desktop apps do on viewing?
Related
we are creating an application which allows image upload, now we want to reduce the image size to reduce the page load time, now this is the code i'm using, it doesn't work, image size remains 100% it doesn't reduce, please help maybe i'm missing something or i'm doing something wrong
if(!empty($_FILES['image_field']['name']))
{
$config['upload_path'] = 'image_folder';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '262144';
$config['file_name'] = 'my_image_name';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('image_field'))
{
$this->session->set_flashdata('error','<div class="alert alert-warning">Something went wrong...try again...</div>');
redirect('Controller/function_name');
}
else
{
$type = array('image/png'=>'png','image/jpg'=>'jpg','image/jpeg'=>'jpg');
$config['image_library'] = 'gd2';
$config['source_image'] = base_url().'image_folder'.$config['file_name'].'.'.$type[$_FILES['image_field']['type']];
$config['quality'] = '50%';
$this->load->library('image_lib', $config);
$this->image_lib->resize();
// save to database
}
}
i'm using codeigniter 3.0
You should use without % as integer like that:
$config['quality'] = 50;
$config['image_library'] = 'gd2';
$config['quality'] = 60;
works great adjust the quality and check the image file size it save. will be different.
I also had to set my image_library parameter to "gd" before the quality change started working. I wonder why it didn't work with "gd2"
$config['image_library'] = 'gd';
$config['quality'] = 40;
I found this would not work for me using the GD or GD2 library.
In order for the "quality" to be affected - I needed to use the "ImageMagick" Library.
As stated in the documentation - if you use either the "ImageMagick" or the "NetPBM" library - "you must supply the path": https://codeigniter.com/userguide3/libraries/image_lib.html
On my GoDaddy server the path was "/usr/bin"
$config['source_image'] = $source_image;
$config['library_path'] = '/usr/bin';
$config['image_library'] = 'ImageMagick';
$config['maintain_ratio'] = TRUE;
$config['quality'] = "20%"; // Maintained the aspect ratio and dimensions but reduced the file size by about 60%
$this->image_lib->initialize($config);
if(!$this->image_lib->resize()){
echo "QUCKVIEW Re-sizing - ";
echo $this->image_lib->display_errors();
echo "Image Resize for Grooming Quickview Failed! Contact System Administrator.";
die();
}
Hi friends I am trying to use codeigniters resize image_lib .. and I am not able to resize the image using this one. Please help me to solve this issue
Error:
using gd:
The path to the image is not correct.Your server does not support the GD function required to process this type of image.
using ImageMagick:
The path to the image is not correct.The path to your image library is not correct. Please set the correct path in your image preferences.
Code:
$this->load->library('upload');
$config['upload_path'] = $path;
$config['file_name'] = $file_name;
$config['allowed_types'] = 'jpg|png';
$config['overwrite'] = false;
$this->upload->initialize($config);
if ( ! $this->upload->do_upload())
{
return false;
}
else{
$this->load->library('image_lib');
$resize['image_library'] = 'gd2';
$resize['source_image'] = $path.$file_name;
$resize['maintain_ratio'] = FALSE;
$resize['width'] = 40;
$resize['height'] = 40;
$resize['quality'] = 100;
// print_r($path.$file_name);
// Here the path of the image is assets/Data/adv_images/2/2-537f2a3651300
// which is absolutely right also tried base_url().$path.$file_name
$this->image_lib->initialize($resize);
if ( ! $this->image_lib->resize()){
echo $this->image_lib->display_errors();
}
return true;
}
try to set path by this way:
$resize['source_image'] = $this->upload->data()['full_path'];
i have a helper function to add watermark to images
(i've made some changes to make the code shorter please ignore minor syntax errors or missing code)
function watermark($path_to_img , $image_name )
{
$ci = &get_instance();
$config['source_image'] = $path_to_img.$image_name;;
$config['new_image'] = $path_to_img.'wtr_'.$image_name;
$config['wm_type'] = 'overlay';
$config['wm_overlay_path'] = base_path('/files/transparent_bar.png');
$config['wm_font_path'] = base_path('application/assets/view/tahomabd.ttf');
$config['wm_font_size'] = '8';
$config['wm_font_color'] = 'ffffff';
$config['wm_vrt_alignment'] = 'bottom';
$config['wm_hor_alignment'] = 'left';
$config['quality'] = '100%';
$config['wm_hor_offset'] = '0';
$config['wm_vrt_offset'] = '0';
$ci->image_lib->initialize($config);
$ci->image_lib->watermark();
}
it works fine but it saves the watermarked version on server , i want to show/download it on the fly without saving it on the server or replacing the original image .... i've no idea how can i do this in codeigniter ?
something like :
$img = watermark($path_to_img , $image_name);
header("Content-type: image/jpeg");
echo ( $img );
You can still save it in a temp directory and have a code removing images older than a few minutes for example. Or harder, have a php script streaming out the image, e.g. the URL should be something like script.php?IMG=flower.jpg
http://thesitewizard.com/php/create-image.shtml for the second option above
Or
http://www.sitepoint.com/watermark-images-php/
To replace the source image, set source image path to new image.
$config['source_image'] = $path_to_img.$image_name;
$config['new_image'] = $path_to_img.$image_name;
I am working on media library for a website at the moment, and one of the features is that user can create a cropped version of an image that they upload.
My problem however is this, when I try and crop the image, I get the following error,
The path to the image is not correct.
Here is my code,
$config['image_library'] = 'gd2';
$config['source_image'] = "/media/images/products/".$this->data['image'];
//die($config['source_image']);
$config['maintain_ratio'] = FALSE;
$config['x_axis'] = $this->input->post('x');
$config['y_axis'] = $this->input->post('y');
$config['width'] = $this->input->post('w');
$config['height'] = $this->input->post('h');
$config['dynamic_output'] = FALSE;
$config['create_thumb'] = TRUE;
$this->load->library('image_lib', $config);
if(!$this->image_lib->crop()) {
if($this->input->post('isAjax') == "1") {
echo json_encode($this->image_lib->display_errors());
} else {
$this->data['image_error'] = $this->image_lib->display_errors();
$this->template->build('/admin/media/crop', $this->data);
}
} else {
$filename = base_url()."media/images/products/".$this->data['image'];
$extension_pos = strrpos($filename, '.'); // find position of the last dot, so where the extension starts
$thumb = substr($filename, 0, $extension_pos) . '_thumb' . substr($filename, $extension_pos);
if($this->input->post('isAjax') == 1) {
echo json_encode($success = array('message' => 'Image Cropped'));
} else {
$this->data['success'] = "Image Cropped";
$this->template->build('/admin/media/crop', $this->data);
}
}
So I though I would change $config['source_image'] to the following,
$config['source_image'] = "./media/images/products/".$this->data['image'];
however that just leaves with this message,
Your server does not support the GD function required to process this type of image.
Am I doing something fundamentally wrong? I am only trying to crop a simple .png, and the file certainly exists on my server, and I most definatly have GD2 installed.
The is most likely just a file path issue (CI is pretty good about accurate, relevant error messages). Here are the steps I would take to resolve it:
var_dump(file_exists($config['source_image']) to see if PHP finds the file. I'd be shocked by a "true" response here.
If the filesystem is case-sensitive, make sure that's not the problem
Check the include path (I assume regular includes are working fine if CI gets this far...)
More of an anti-step, but DON'T use $_SERVER['DOCUMENT_ROOT'], as a commenter suggests. FC_PATH (or other CI-defined constants) should get you the base path you need.
Happy hunting.
I'm struggling to get the CodeIgniter Image Manipulation working correctly. Either it's a bug or I'm just not seeing it. I hope someone can help me with it. Thanks in advance!
On the script: I want to create thumbnails (176w x 132h). The input images are in different sizes and ratios. In order to always get this size I first resize them to fit the max width or height (depending on image orientation) and then crop in the center.
I've tried to do it all in 1 method. That didn't work, so I created two seperate methods.
resize_img()
and
crop_img().
When I run resize_img() on 3 different files, it works. If after that I use crop_img() on these thumbnails the 1th method created, it works. If I combine the two, or use them after one another, it doesn't.
I've tried $this->image_lib->clear();, unsetting the config files. I even created two config files, just to be sure.
I'm getting different all kind of errors from GD2, but the problem is, that after resize_img() creates the thumbnail, the crop_img() won't crop it. After that it all goes south, and the next images can't be opened. Write premissions are checked, both on folder and files.
Unable to save the image. Please make sure the image and file
directory are writable. The path to the image is not correct. Your
server does not support the GD function required to process this type
of image.
Full code:
<?PHP
class Imagetest extends MY_Controller {
function __construct()
{
parent::__construct();
$this->load->library('image_lib');
}
function index()
{
$testimg1 = 'uploads/test/1.png';
$testimg2 = 'uploads/test/2.png';
$testimg3 = 'uploads/test/3.png';
$this->resize_img($testimg1);
$this->crop_img($testimg1);
$this->resize_img($testimg2);
$this->crop_img($testimg2);
$this->resize_img($testimg3);
$this->crop_img($testimg3);
}
function image_thumb_name($img = null)
{
if(!empty($img)) {
$exploded = explode('.', $img);
return $exploded['0'] . '_thumb.' . $exploded['1'];
}
}
function get_axis($img)
{
// Default values
$output['x_axis'] = 0;
$output['y_axis'] = 0;
// Settings
$config['height'] = 132;
$config['width'] = 176;
if ($img_dim = getimagesize($img)) {
list($thumb_width, $thumb_height) = $img_dim;
} else {
echo '<h1> ERROR HERE TOO</h1>';
return false;
}
if ($thumb_width > $config['width']) {
$output['x_axis'] = (($thumb_width - $config['width']) / 2) ;
} else if ($thumb_height > $config['height']) {
$output['y_axis'] = (($thumb_height - $config['height']) / 2);
}
return $output;
}
function resize_img($img)
{
$config = array();
echo 'Processing: '. $img .'<br/>'; // Debug
if ($img_dim = getimagesize($img)) {
list($image_width, $image_height) = $img_dim;
} else {
echo '<h1> ERROR HERE </h1>';
}
// create a thumbnail
$config['image_library'] = 'GD2';
$config['source_image'] = $img;
$config['quality'] = 100;
$config['height'] = 132;
$config['width'] = 176;
$config['create_thumb'] = TRUE;
$config['maintain_ratio']= TRUE;
$config['master_dim'] = ($image_width > $image_height) ? 'height' : 'width';
$this->image_lib->initialize($config);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
echo '<img src="../'.$this->image_thumb_name($img).'" /> <br/>'; // Debug
$this->image_lib->clear();
unset($config);
}
function crop_img($img)
{
$config2 = array();
// Crop that thumbnail
$config2['image_library'] = 'GD2';
$config2['quality'] = 100;
$config2['height'] = 132;
$config2['width'] = 176;
$config2['source_image'] = $this->image_thumb_name($img);
$axis = $this->get_axis($config2['source_image']);
$config2['x_axis'] = $axis['x_axis'];
$config2['y_axis'] = $axis['y_axis'];
$config2['maintain_ratio'] = FALSE;
$config2['create_thumb'] = FALSE;
$this->image_lib->initialize($config2);
if (!$this->image_lib->crop()) {
echo $this->image_lib->display_errors();
}
echo '<img src="../'.$config2['source_image'].'" /> <br/>'; // Debug
$this->image_lib->clear();
unset($config2);
}
}
Got it!
I've set the create_thumb option to FALSE, and used the new_image parameter in the resize_img method. The effect is the same, but the built-in create_tumb function is not being used.
It's a bug IMHO, but it's working now :)
$config['create_thumb'] = FALSE;
$config['new_image'] = $this->image_thumb_name($img);
The problem is with your $config['source_image']
var_dump($config['source_image']);
see what you get.
If the file is in images folder in root folder, the $config['source_image] = 'images/'.$img.
Also make sure all the exploding functions returning the correct file name. (return and echo to check).
1st, eliminate file not found error. After that, see what error comes.
You can use Kodem's Image Resize Service. You can resize, crop any image with just a http call. Can be used casually in the browser or used in your production app.