First you must have an upload form in your HTML, notice that the file input field in the form below is profile_pic
1. 2. 3. 4.
In Controller, create an instance of DooGdImage, along with the upload/source path and the path to save your resized pictures:
1.
Doo::loadHelper(‘DooGdImage’);
2.
//upload/source path, and output saved path
3.
$gd = new DooGdImage(‘/var/www/uploaded/’, ‘/var/www/resized_pic/’);
Call uploadImage() to save the uploaded file with a new name. The example below save the picture with the date as file name, img_20100104203245.jpg
1.
$uploadImg = $gd->uploadImage(‘profile_pic’, ‘img_’ .date(‘Ymdhis’));
Before your resize the picture, you can set the quality, generated image type and file name suffix (optional)
1.
$gd->generatedQuality = 85;
2.
$uploadImage->generatedType=”jpg”;
3.
4.
//This thumbnail is 46×46 pixels, resize adaptively (perfect 46×46 crop from center)
5.
//Pic name is img_201001011200_46x46.jpg
6.
$gd->thumbSuffix = ‘_46x46’;
7.
$gd->adaptiveResize($uploadImg,46,46);
You can use createThumb/createSquare to resize the pictures too.
1.
//Resize propotionally (so will not be perfect 75×75 depends on the image ratio, no cropping done)
2.
//Pic name is img_201001011200_75x75.jpg
3.
$gd->thumbSuffix = ‘_75x75’;
4.
$gd->createThumb($uploadImg, 75, 75);
Some updates*
You can validate if the uploaded images meet your requirements by using checkImageType(), checkImageSize() and checkImageExtension()
checkImageType() – check if image mime type is in the allowed list. Default: JPEGs, GIFs and PNGs
checkImageExtension() – check if image file extension is in the allowed list.