wrap phpThumb in a CodeIgniter controller method
-
14-11-2019 - |
Question
On Codeigniter, I'm using phpThumb to generate images. I didn't use the built-in CI image library because it couldn't do the "zoom-crop" feature of phpThumb, which I badly need (ability to create thumbnails of the same size regardless of their original size/proportions, by zooming and cropping when necessary).
Everything is working fine except my images' src
look so ugly:
<img src="lib/phpThumb.php?src=../../upload/photo/file.jpg&w=100&h=100&zc=1" />
I was hoping to make it look something like this instead:
<img src="photo/display/file.jpg" />
by wrapping phpThumb in a CI controller photo
with the method display($filename) {}
I was able to do this using the built-in CI image library, but I'm at a loss on how to use phpThumb instead.
Any suggestions?
Solution 2
I found a different PHP thumbnailing library (also called PHPThumb) and used it: https://github.com/masterexploder/PHPThumb/wiki
Here's my CodeIgniter class:
class Photo extends CI_Controller {
function __construct() {
parent::__construct();
}
function display($filename) {
require_once('./lib/PHPThumb/ThumbLib.inc.php');
$thumb = PhpThumbFactory::create('./upload/photo/' . $filename);
$thumb->adaptiveResize(100, 100);
$thumb->show();
}
}
Now my img
looks better:
<img src="photo/display/file.jpg" />
OTHER TIPS
If you are using the apache web server then apache will try to serve up an image if a url with a .jpg extension is requested. So you can do this 2 ways:
1) Change your .htaccess file to tell apache to route all requests to a specific image name within a folder to a controller/method in your CI app which does the phpThumb rendering.
or, better yet
2) Have phpThumb save the images to a location on your server instead of rendering them dynamically. Then you can just access the file name that phpThumb saves to.