Zend and phpThumb - Upload Images of a given product or article
Let's say I know how to upload an image using Zend开发者_如何学Python. Let's say I know how to generate the thumbs of that image using phpThumb class integrated with Zend.
Where (controller, helper?) should we then associate that image with a given database record? Should or product or article database table have a field called img path and another called thumb path ? Or should we store the images on a the database?
I'm not looking into specifics here, just a general way of doing it, so that I could understand what is a common way of archieving it.
Thanks a lot
We usually have some concrete directory for saving images (like /public_html/images) and there we store the originals (usually resized to some max. size - say 1024px - upon uploading). There are also subdirectories there, called medium and small (for product detail and small thumbnail for category list).
We also store the images using products primary key (if products:images is 1:1) or using PK + id (for 1:N). Then we easily assemble the url in img() view helper like this.
public function img($product, $imageId, $size = self::SMALL) {
return sprintf('/images/%s/%s_%s.jpg', $size, $product->id, $imageId);
}
//in view - can also be wrapped in view helper - depends on your scenario
<a href="<?php echo $this->img($product, 1, 'big');?>"><img src="<?php echo $this->img($product, 1, 'small');?>" /></a>
Edit: BTW: Actually we never use the originals. We make "big" sized images.
The action helper for image resizing is generally a good idea. But depends on how you upload your images - if there is only one action and one controller - there is no need AFAIK. But you may want to upload images also from import, using FTP upload, etc.
The linking (if the process described abowe doesn't work well for you) of images via DB falls to two categories - content centric and image centric.
You can either go into product editing, where is a button or like "upload a new image" or "link uploaded image". Other option is to have a list of images with "connect to a product". Highly dependable on your users' workflow.
Also there are some options how to build your db. You can have image_id in your product table. Or you can have imalelink_id in you product table, which links to M:N table that connects images to products. Also you can have product_id in your image table :) All of these approaches are valid in some cases and wrong in some other. All depends on how the end users will use it.
I usually do this:
- Save the path of the original image in a mysql varchar field
- Create a resize controller
- Disable the view and the layout from the controller
- Get the path of the image via $this->_getParam('img');
- Validation
- Resize the image
- Show the image
- Inside a view script i call img src="/resize?img=image.jpg"
edit: Usually, in combination with this, i write a custom route to avoiding the ugly url being displayed in the source code of the page
精彩评论