Welcome Guest, Not a member yet? Register   Sign In
image manipulation, resize adn unique name
#21

[eluser]LynxCoder[/eluser]
[quote author="the_unforgiven" date="1334317009"]All images are uploaded to uploads folder the orginals.

Thumbs are uploaded to uploads/thumbs then the filename which will be for example:

aimage_thumb.png

whereas the orginal will just be aimage.png

soo what i want to do using this model i have you will see the commented out line is what i want to achieve:

Code:
function addItem(){
  $data = array(  
   'category_id' => $this->input->post('categories'),
   'customerNumber' => $this->input->post('customerNumber'),
   'item_name' => $this->input->post('name'),
   'item_description' => $this->input->post('desc'),
   'item_price' => $this->input->post('price'),
   'posted_by' => $this->input->post('customer'),
  
  );
  $image_data = $this->upload->data();
  $data['item_img'] = $image_data['file_name'];
  // So i presume i will need $data['item_img'] = $image_data['file_name']; again but with thumb in the $data not item_img but what will go inside the $image_data? if i put file_name again it just puts in the original image but i want it to store the thumb that was created.

  $this->db->insert('items', $data);
  }
[/quote]

Ah right ok. Well first thoughts are that I always look to minimise the amount of data i'm storing in the database where possible - the smaller the database size the better. In your case the thumbs and original images always have the same filename (just the thumbs have '_thumb' on the end, so I would just store the original filename in the database, then in your view file when you want the thumbnail to be displayed just use str_replace

FOR EXAMPLE:
Code:
<img src="&lt;?php echo $img_path . str_replace('.jpg','_thumb.jpg',$data['item_img']);?&gt;">

and that will display the thumbnail.

If you wanted to be really efficent and all your images are JPG files, you could even remove the .jpg from the original filename and just add it back in the view, so that you would use the following instead:

Code:
// remove the .jpg from the filename
$data['item_img'] = str_replace('.jpg','',$image_data['file_name']);


VIEW CODE:
// display the original image
<img src="&lt;?php echo $img_path . $data['item_img'];?&gt;.jpg">

// display the thumbnail
<img src="&lt;?php echo $img_path . $data['item_img'];?&gt;_thumb.jpg">

That would be my preferred option. Other than that - yes your good to go!

Rich
#22

[eluser]the_unforgiven[/eluser]
like so in the screen shot so i can just pull the related thumb from the db then.



http://imageshack.us/photo/my-images/31/...at125.png/
#23

[eluser]LynxCoder[/eluser]
[quote author="the_unforgiven" date="1334318107"]like so in the screen shot so i can just pull the related thumb from the db then.
http://imageshack.us/photo/my-images/31/...at125.png/[/quote]

Sorry, just editted the previous post as the forum kept stripping data out of it!

Yes that would work fine, but your duplicating data by the original and thumb entries, when you dont need to, what is called bad programming practice. But yes, it will work fine.

Rich
#24

[eluser]the_unforgiven[/eluser]
Yes but i put that in manual via phpmyadmin is what i mean so how can i strip it so it just puts in raw file name and raw thumb so for example:

I upload an image with any extention say png

its called mylittleimage.png which gets saved it also creates mylittleimages_thumb.png at the same time and stores this to a database firstly what do i need to do to get this to work like i have just explained then i can just echo out a var with thumb on and large orginal image if i want to.

Basically im building a small items for sale website for a customer and they need to be able to uplad images to be reseized which it currently does now so that on the front end home page the list of all items from different people all have a thumb image showing and not the orgianl otherwise the whole design would be cocked up.

So it would show the thumb when that product is clicked it will then show the orginal image on a larger scale along with product title and description, but do you see where am going with this?
#25

[eluser]the_unforgiven[/eluser]
also not all files uploaded are going to be one extention some will be jpg some will be gif, png jpeg etc so need to have the ability to do which ever format.
#26

[eluser]LynxCoder[/eluser]
[quote author="the_unforgiven" date="1334319014"]also not all files uploaded are going to be one extention some will be jpg some will be gif, png jpeg etc so need to have the ability to do which ever format.[/quote]

Ok, well there are a number of ways in which it can be done. Possibly the easiest (although not the best) is to use strrpos (http://php.net/manual/en/function.strrpos.php) to find the last '.' in the string, so you have something like

Code:
$ext_start = strrpos($filename,'.') - 1;
$filename = substr($filename,$start,$ext_start);

That will give you the filename minus the extension. Then do

Code:
$file_ext = substr($filename,$ext_start,strlen($filename));

That will give you the extension. So store both in the database in separate fields and you can then do what you want to do.


#27

[eluser]the_unforgiven[/eluser]
Ok maybe i'll try that
#28

[eluser]the_unforgiven[/eluser]
So would this go in the controller or model? and do i rename the $filename to somrthing else?
#29

[eluser]LynxCoder[/eluser]
[quote author="the_unforgiven" date="1334325521"]So would this go in the controller or model? and do i rename the $filename to somrthing else?[/quote]

In the controller really, although the part where your stripping the extension off the filename could go in the model if you wanted it to.

Rich
#30

[eluser]the_unforgiven[/eluser]
Ok ill give that a whirl then see what happens




Theme © iAndrew 2016 - Forum software by © MyBB