When I resize an image keeping $config['create_thumb'] = true Codeigniter adds the string "_thumb" in the end of the name of resized image.
What I want ask is, is it possible to add the suffix "_thumb" in the beginning of the image name
example:
original_name.jpg
after resizing:
original_name_thumb.jpg
What I want:
thumb_original_name.jpg
Any kind of help is appreciated!
You can try this:
$data = $this->upload->data();
$config['create_thumb'] = false;
$config['new_image'] = 'thumb_'.$data['file_name'];
And if you have other problems with CI image manipulation library, maybe you can try ImageMoo.
If u don't want to add the suffix _thumb in thumbnail file name then
use $config['thumb_marker'] = false;
this can be achieved by following way
$data = $this->upload->data();
$config['create_thumb'] = false;
$config['new_image'] = 'thumb_'.$data['file_name'];
for more information about re-size image check out following Code.
function special_resize($oldFile, $width, $height)
{
$obj =& get_instance();
$info = pathinfo($oldFile);
out($info);
$tempFile = '/public/files/files/temp/' . $info['filename'] . '-' . $width . 'x' . $height . '.' . $info['extension'];
$newFile = '/public/files/files/cache/' . $info['filename'] . '-' . $width . 'x' . $height . '.' . $info['extension'];
//if image already exists, use it
if(file_exists('.' .$newFile))
return $newFile;
//math for resize/crop without loss
$o_size = _get_size( '/' . $info['dirname'] . '/' . $info['basename']);
$master_dim = ($o_size['width']-$width < $o_size['height']-$height?'width':'height');
$perc = max( (100*$width)/$o_size['width'] , (100*$height)/$o_size['height'] );
$perc = round($perc, 0);
$w_d = round(($perc*$o_size['width'])/100, 0);
$h_d = round(($perc*$o_size['height'])/100, 0);
// end math stuff
/*
* Resize image
*/
$config['image_library'] = 'gd2';
$config['source_image'] = $oldFile;
$config['new_image'] = '.' . $tempFile;
$config['maintain_ratio'] = TRUE;
$config['master_dim'] = $master_dim;
$config['width'] = $w_d + 1;
$config['height'] = $h_d + 1;
$obj->image_lib->initialize($config);
$obj->image_lib->resize();
$size = _get_size($tempFile);
unset($config); // clear $config
/*
* Crop image in weight, height
*/
$config['image_library'] = 'gd2';
$config['source_image'] = '.' . $tempFile;
$config['new_image'] = '.' . $newFile;
$config['maintain_ratio'] = FALSE;
$config['width'] = $width;
$config['height'] = $height;
$config['y_axis'] = round(($size['height'] - $height) / 2);
$config['x_axis'] = 0;
$obj->image_lib->clear();
$obj->image_lib->initialize($config);
if ( ! $obj->image_lib->crop())
{
echo $obj->image_lib->display_errors();
}
$info = pathinfo($newFile);
out($info);
return $newFile;
}
Related
i want to resize image in codeigniter in one function
i called Image_lib and upload libraries on my controller
the images folder gets read/write .
That is my controller.
public function update(){
$config['upload_path']="./images/";
$config['allowed_types']='jpg|jpeg|png';
$config['encrypt_name']=TRUE;
$config['max_size']='1024';
$this->load->library('upload',$config);
if(!$this->upload->do_upload()){
$this->load->view('settings_v',array(
'userdata'=>$this->user->userdata(),
'c_data'=>$this->Settings_m->index(),
'error'=>$this->upload->display_errors(),
));
} else{
$image=$this->Settings_m->image(); // get current image to remove it
if(!empty($image)){
$path='images/'.$image;
unlink($path);
}
$config['image_library'] = 'gd2';
$config['source_image'] = '.images/'.$filename;
$config['new_image'] = '.images/'.$filename;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 30;
$config['height'] = 30;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$file_data=$this->upload->data();
$filename=$file_data['file_name'];
$this->Settings_m->update($filename);
//redirect('Settings','refresh');
}
}
Thanks ,
Change '.images/ to './images/' also you never defined $filename before using it in image lib config.
$file_data = $this->upload->data();
$filename = $file_data['file_name'];
$config['image_library'] = 'gd2';
$config['source_image'] = './images/' . $filename;
$config['new_image'] = './images/' . $filename;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 30;
$config['height'] = 30;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->Settings_m->update($filename);
//redirect('Settings','refresh');
Also if you just want to resize the source image and not have a separate resized image you should remove $config['new_image'] and $config['create_thumb'] as per the docs. Otherwise just use create_thumb or new_image (with a different path/filename than the original) but not both.
Notes regarding this preference:
If neither of the two preferences listed above (create_thumb, and
new_image) are used, the resizing method will instead target the
original image for processing.
I tried hard, just wanna display barcode image in A1 cell. it works well in html. But not in PhpExcel. thanks in advance.
$generator = new BarcodeGeneratorPNG();
$wizard = new PHPExcel_Helper_HTML();
$cellText = '<img src="data:image/png;base64,' . base64_encode($generator->getBarcode('081231723837', $generator::TYPE_CODE_128)) . '">';
$richText = $wizard->toRichTextObject($cellText);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $richText);
Try whith:
https://github.com/davidscotttufts/php-barcode
generating barcode.png
$filepath = 'barcode.png';
$text = '1234567890123';
$size = "80";
$orientation = "horizontal";
$code_type = "code128";
$print = "true";
$sizefactor = "0.8";
barcode( $filepath,$text, $size, $orientation, $code_type, $print, $sizefactor );
Report:
$imgBarcode = imagecreatefrompng('bar_code.png');
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setDescription('barcode');
$objDrawing->setImageResource($imgBarcode);
$objDrawing->setHeight(100);
$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
I'm working on a website wich i haven't create/developed
Anyway users can upload image and when they do that , there's a function that create a duplicate of that image with a watermark
But the copy with the watermark has low quality and also it's size is much smaller than original image
I dont see anything lowering the quality , maybe it's how CI watermark works ?!
without watermark
http://img.akstube.ir/images/2013/05/DSC_0168_69_70_tonemapped.jpg
with watermark
http://img.akstube.ir/images/2013/05/irwm_DSC_0168_69_70_tonemapped.jpg
here is the function
function ir_watermark($file,$name='',$rebuild=FALSE)
{
$ci = &get_instance();
$pathinfo = pathinfo($file);
$filename = $pathinfo['basename'];
$path = $pathinfo['dirname'];
$new_image = $path . '/irwm_'. $filename;
$path_to_img = base_url(str_replace(base_path(),'',$new_image));
if ($rebuild == FALSE)
if (file_exists($new_image))
{
if ($ci->uri->segment(1) == 'test')
echo "<img src='{$path_to_img}' />";
return $path_to_img;
}
$config['source_image'] = $file;
$config['new_image'] = $new_image;
$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['wm_hor_offset'] = '0';
$config['wm_vrt_offset'] = '0';
$ci->image_lib->initialize($config);
$ci->image_lib->watermark();
$config['new_image'] = $config['new_image'];
$config['source_image'] = $config['new_image'];
$config['wm_hor_offset'] = '10';
$config['wm_vrt_offset'] = '2';
$config['wm_overlay_path'] = base_path('/files/akstube_logo6.png');
$ci->image_lib->initialize($config);
$ci->image_lib->watermark();
$config['wm_type'] = 'text';
$config['wm_vrt_offset'] = '-3';
$config['wm_text'] = 'Photo : ' . $name;
$config['wm_text'] = strtoupper($config['wm_text']);
$config['wm_hor_alignment'] = 'right';
$config['wm_hor_offset'] = '-50%';
$ci->image_lib->initialize($config);
$ci->image_lib->watermark();
if ($ci->uri->segment(1) == 'test')
echo "<img src='{$path_to_img}' />";
return $path_to_img;
}
WATERMARK IMAGE
http://akstube.ir/files/akstube_logo6.png
http://akstube.ir//files/transparent_bar.png
again I Throw a problems. How Can i find new_image (Thumb) name.
In Controller
//Insert image .............................
$config['upload_path'] = './img/placeholders/blog';
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->upload->initialize($config);
$this->upload->do_upload('blog_images');
//Thumb Image-----------------------------
$image_des = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = './img/placeholders/blog/' . $image_des['file_name'];
$config['maintain_ratio'] = TRUE;
$config['create_thumb'] = TRUE;
$config['new_image'] = './img/placeholders/blog/thumb/' . $image_des['file_name'];
$config['width'] = 220;
$config['height'] = 140;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$image_dev = $this->upload->data();
// How Can i find new image name.
$data['blog_img'] = 'img/placeholders/' . $image_des['file_name'];
$data['blog_thumbs'] = ?
after convert,
blog_img name is "e7da891c90586f7c4e89ef5724891a15.jpg"
blog_thumbs is "e7da891c90586f7c4e89ef5724891a15_thumb.jpg"
How Can i find new_image (Thumb) name. like $image_des['file_name']
But you already have it right!
$image_des['raw_name'] = 'e7da891c90586f7c4e89ef5724891a15'; //File name without extension.
$image_des['file_ext'] = '.jpg'; //The file extension with period
$data['blog_thumbs'] = 'your/preceding/path/'.
$image_des['raw_name'].'_thumb'.$image_des['file_ext'];
Refer file uploading class for explanation about return array value of $this->upload->data().
I think this function isn't as efficient as it should be. I'd appreciate some suggestions on how I can structure it to be faster and take less memory. This is what the code does:
checks to see if the image was uploaded
add details about it (tags, name, details) to the database
if the variable $orientation was set, rotate the image
if the image is wider than 600px, resize it
create a thumbnail
I think the inefficiencies come from having steps 3,4,5 all separate. Is there any way to consolidate them? Thanks!
function insert()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg';
$config['max_size'] = '5000';
$config['max_width'] = '4096';
$config['max_height'] = '4096';
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
$data = array('error' => $this->upload->display_errors());
$data['title'] = "Add Photo | Mark The Dark";
$this->load->view('photo_add_view', $data);
}
else
{
//get uploaded image info
$data = array('upload_data' => $this->upload->data());
//clean the data
$data = $this->input->xss_clean($data);
//get orientation info and erase it from POST variable
//$orientation = $_POST['orientation'];
//unset($_POST['orientation']);
//grab the tags
$tags = $_POST['tags'];
unset($_POST['tags']);
//add in some other stuff
$_POST['time'] = date('YmdHis');
$_POST['author'] = $this->dx_auth->get_user_id();
//insert it in the database
$this->db->insert('photos', $_POST);
$photo_id = $this->db->insert_id();
//add stuff to tags table
/*
$tags_array = preg_split('/[\s,;]+/', $tags);
foreach($tags_array as $tag)
{
if($tag != "" || $tag != null)
$this->db->insert('tags', array('id' => $photo_id, 'word' => $tag));
}*/
//CXtags
/*$tags_array = preg_split('/[\s,;]+/', $tags);
foreach($tags_array as $tag)
{
if($tag == "" || $tag == null)
{unset($tags_array[$tag]);}
}
*/
$tags_array = $this->CXTags->comma_to_array($tags);
foreach($tags_array as $tag)
{$tags_array[$tag] = $this->CXTags->make_safe_tag($tag);}
$topass = array(
'table' => 'photos',
'tags' => $tags_array,
'row_id' => $photo_id,
'user_id' => $_POST['author']
);
$this->CXTags->add_tags($topass);
//rename the file to the id of the record in the database
rename("./uploads/" . $data['upload_data']['file_name'], "./uploads/" . $photo_id . ".jpg");
list($width, $height, $type, $attr) = getimagesize("./uploads/" . $photo_id . '.jpg');
if (($orientation == 1) || ($orientation == 2))
{
//echo $orientation;
//rotate image
$config['image_library'] = 'GD2';
$config['source_image'] = './uploads/' . $photo_id . '.jpg';
if ($orientation == 1)
{
$config['rotation_angle'] = 270;
}
elseif ($orientation == 2)
{
$config['rotation_angle'] = 90;
}
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
if(!$this->image_lib->rotate())
{
echo $this->image_lib->display_errors();
}
}
$this->load->library('image_lib');
if ($width > 600)
{
//resize image
$config['image_library'] = 'GD2';
$config['source_image'] = './uploads/' . $photo_id . '.jpg';
$config['new_image'] = './uploads/photos/' . $photo_id . '.jpg';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 600;//180
$config['height'] = 480;
$config['master_dim'] = 'width';
$this->image_lib->initialize($config);
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
}
else
{
$source = './uploads/' . $photo_id . '.jpg';
$destination = './uploads/photos/' . $photo_id . '.jpg';
rename($source, $destination);
/*//buggy php???
$result = copy($source, $destination);
echo "HO" . $result;
*/
}
//create thumbnail
$config['image_library'] = 'GD2';
$config['source_image'] = './uploads/photos/' . $photo_id . '.jpg';
$config['new_image'] = './uploads/thumbnails/' . $photo_id . '.jpg';
$config['create_thumb'] = TRUE;
$config['thumb_marker'] = '_thumb';
$config['maintain_ratio'] = TRUE;
$config['width'] = 180;//180
$config['height'] = 100;
$config['master_dim'] = 'width';
$this->image_lib->initialize($config);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
redirect('photo/show/' . $photo_id);
}
//redirect('photo/photo_add/');
}
Well, I can answer part of your question - you can rotate images which carry the exif rotation using Codeigniter.
Firstly:
Detect the uploaded images exif data.
Look at the $exif array and handle the Orientation item.
Pass the required rotation degrees to codeigniters image_lib->rotate() function.
EG:
public function auto_rotate_image($upload_data){//iphone rotation fix
$path = $upload_data['full_path'];
$exif = exif_read_data($path);
if(isset($exif['Orientation'])){
$rotate = false;
switch($exif['Orientation']){//only really interested in the rotation
case 1: // nothing
break;
case 2:// horizontal flip
break;
case 3: // 180 rotate left
$rotate = 180;
break;
case 4: // vertical flip
break;
case 5: // vertical flip + 90 rotate right
break;
case 6: // 90 rotate right
$rotate = 270;
break;
case 7: // horizontal flip + 90 rotate right
break;
case 8: // 90 rotate left
$rotate = 90;
break;
}
if($rotate){
$config=array();
$config['image_library'] = 'gd2';
$config['source_image'] = $path;
$config['rotation_angle'] = $rotate;
$config['overwrite'] = TRUE;
$this->load->library('image_lib',$config);
if(!$this->image_lib->rotate()){
echo $this->image_lib->display_errors();
}
}
}
}
For all the work required to do your image manipulation using the objects that code igniter exposes for you, you may want to look into doing this yourself strictly with the gd functions in php. I have not used code igniter, but I've done a lot of stuff with image manipulation in php, and it's not terribly difficult.
Just by looking at your code, and I'm assuming this is the code igniter way, I see you calling image_lib->initialize() and load->library() for each individual manipulation. I'd have a look inside those methods and the rotate() and resize() methods you're using and see if they're creating and destroying the image resource with each manipulation. If you use the gd library, you can reuse the same image resource for each step and then just write it to file when you're ready (re-use the same image resource to create the thumbnail). This would probably make a huge performance gain in both execution speed and memory used.
GD Functions in PHP
You could store your configs elsewhere per codeigniter documentation, that would tighten the code up. I don't know how you could chain those events.