I want to create a thumbnail of a PDF. I am not able to create thumbnail. In raw PHP it works but in Joomla it's not working. Please let me know what I am doing wrong.
$uploadPath = JPATH_ADMINISTRATOR . '/components/com_ets_fast_track/assets/buildings/' . $filename;
$fileTemp = $file['tmp_name'];
if(!JFile::exists($uploadPath)){
//name the thumbnail image the same as the pdf file
$pdfWithPath = $uploadPath;
$thumbDirectory = JPATH_ADMINISTRATOR . '/components/com_ets_fast_track/assets/buildings/';
$thumb = basename($filename, ".pdf");
//add the desired extension to the thumbnail
$thumb = $thumbDirectory.$thumb.".jpg";
//execute imageMagick's 'convert', setting the color space to RGB and size to 200px wide
exec("convert $pdfWithPath[0] $thumb");
}
Related
I have been trying to use imagemagic in my codeigniter API. The problem is that below code works fine with my jpg image compression. When I upload ".jpg" image of 2MB size it successfully resizes it to perfect 80kb file on server but when I use ".png" extension the size goes down but not that much it still gives me image size of 300kb and I observed that with png format "quality" does not effect. I tried putting quality 100 and 50 but still same result of big image size for ".png".
And also that if I remove "width" and "height" config parameter than only "quality" does not affect the image file it uploads image of very big size... no image file size compression bases on "quality" parameter.
Can anyone help where I am making mistake or missing something???
My API:
public function multi_image_upload()
{
$aa = array();
if (!empty($_FILES['images'])) {
foreach ($_FILES['images']['name'] as $key => $image) {
$extFile = $_FILES['images']['name'][$key];
$ext = pathinfo($extFile, PATHINFO_EXTENSION);
$nm = date('ymdhis') . '-bingo' . rand(11111, 99999);
$filnm = $nm . ".png";
$file = APPPATH . '../upld/biz/' . $filnm;
$newname = $filnm;
$source_image = $file;
if (move_uploaded_file($_FILES['images']['tmp_name'][$key], $file)) {
$this->fun->imageupload("upld/biz/", $newname, $source_image);
if (file_exists($file)) {
unlink($file);
}
}
}
$res = $this->responce(false, $aa);
} else {
$res = $this->responce(true, "Please select image");
}
echo $res;
}
IMAGE UPLOAD MODEL:
public function imageupload($url, $newname, $source_image)
{
$path = APPPATH . '../' . $url;
$imgsz = array(256,700);
foreach ($imgsz as $imxs) {
$sizefolder = $imxs . "x" . $imxs . "/";
if (!file_exists($path . $sizefolder)) {
mkdir($path . $sizefolder, 0777);
}
$size = getimagesize($source_image);
$filekb = filesize($source_image); // $config['image_library'] = 'gd2';
$config['image_library'] = 'ImageMagick';
$config['library_path'] = '/usr/bin/';
$config['overwrite'] = TRUE;
$config['source_image'] = $source_image;
$config['new_image'] = $path . $sizefolder . $newname;
$config['quality'] = 100;
if ($size[1] > $size[0]) {
$config['height'] = $imxs;
} else {
$config['width'] = $imxs;
}
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
}
JPEG is a lossy compression, meaning that you can limit the size of the output image by setting quality settings, resulting in lower quality image and also lower file size.
PNG is a lossless compression, so you cannot limit quality - it always stays the same. But you can limit compression - the effort that Imagemagick puts into making the PNG file smaller - but like with e. g. ZIP compression the output size is largely dependent on data type - and most images do not compress particularly well.
Also see Compress a PNG image with ImageMagick for some more options on limiting PNG file sizes.
I have simple resize image function in my laravel project. It should upload original image and make thumbnail of it.
After form submitting I got two images but the original image is placed in wrong directory. This is the function in my controller
if (Input::hasFile('image') && !Input::get('remove_image')) {
$file = Input::file('image');
$filename = str_random(20);
$image = new ImageResize($file);
$original = $filename . '.'. $file->getClientOriginalExtension();
$thumb = $filename . '-thumb.' . $file->getClientOriginalExtension();
$file->move(public_path() . '/uploads', $original);
$imagePath = '/uploads/' . $original;
$thumbPath = '/uploads/' . $thumb;
$image->crop(200, 200);
$image->save('uploads/' . $thumb);
}
Basically when I upload image.jpg I get two images image.jpg and image-thumb.jpg. Both images should be save in uploads i.e. public/uploads/ BUT only thumbnail is saved there.
The original image is saved in **bootstrap**/uploads/. Why is going in bootstrap... directory? I didn't mentioned it anywhere?
You can try to replace the public_path() method to url('/'). Not sure this will help but I don't have good experiences with public_path()
Image::make($request->file('image'))->resize(462, 462)->save('upload_path/filename.jpg'));
Try This Code..
Use Image Intervention to resize and save the image
Image::make($avatar)->resize(250, 250)->save(public_folder('/uploads/'.$filename));
The resize function will resize the image and save function will save the image to uploads folder. Give a desired filename to $filename variable.
Leave only the directory to where you want to save the original. Try to change this line which is moving the image
$file->move(public_path() . '/uploads', $original);
with this one ( remove the public path )
$file->move('uploads', $original);
i have a problem with uploading images into different directory.
$path = "../uploads/";
$path2 = "../uploads2/";
$imagename = $_FILES['photoimg']['name'];
$actual_image_name = $imagename;
$uploadedfile = $_FILES['photoimg']['tmp_name'];
$widthArray = array(600,240); //resize width.
foreach($widthArray as $newwidth)
{
$filename = $uploadedfile,$path,$actual_image_name,$newwidth;
//Original Image
if(move_uploaded_file($uploadedfile, $path.$actual_image_name))
{}
if(move_uploaded_file($uploadedfile, $path2.$actual_image_name))
{}
i want to upload image into uploads and uploads2 folders also?
for example width width = 600px into uploads, width = 240px into folder upload2.
what's wrong with my code?
After moving the file with move_uploaded_file it isn't available in the location stored in $uploadedfile anymore. For the second file you have to use copy function.
Please try the following:
if(move_uploaded_file($uploadedfile, $path.$actual_image_name))
{}
if(copy($path.$actual_image_name, $path2.$actual_image_name))
{}
Remove this line. I don't know for what purpose it's there.
$filename = $uploadedfile,$path,$actual_image_name,$newwidth;
To resize the uploaded image use any library to resize it then pass it.
But here is the full code to upload in different directory. But you will have to resize these two $file1 & $file2 to your expected resize file and replace the same in $file1 & $file2
To resize you can use any code suggested here
$path1 = "../uploads/";
$path2 = "../uploads2/";
$file1= $_FILES['photoimg'];
$file2= $_FILES['photoimg'];
$file1_imagename = $file1['name'];
$file2_imagename = $file2['name'];
$file1_actual_image_name = $file1_imagename;
$file2_actual_image_name = $file2_imagename;
$file1_uploadedfile = $file1['tmp_name'];
$file2_uploadedfile = $file2['tmp_name'];
$widthArray = array(600, 240); //resize width.
if (move_uploaded_file($file1_uploadedfile, $path1 . $file1_actual_image_name)) {
echo "Uploaded Successfully!";
}
if (move_uploaded_file($file2_uploadedfile, $path2 . $file2_actual_image_name)) {
echo "Uploaded Successfully!";
}
I have a question on how to filter image while moving the file. I used uploadify to upload image. What I did is, before he move the image to the directory, the code filter will covert the image to grayscale.
Here is my code
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
$newImg = imagefilter($tempFile, IMG_FILTER_GRAYSCALE); // This is what I insert
move_uploaded_file($newImg,$targetFile);
echo "1";
}
The code is uploadify.php and I just inserted a filter to make it grayscale. Please help me. Thanks in advance.
Imagefilter works on an image resource, not a file, and also a boolean, not a new image. It's probably worth reading through the documentation, but you'll need to change your code to something along these lines
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
// Create an image resource - exact method will depend on the image type (PNG, JPEG, etc)
$im = imagecreatefrompng($tempFile);
// Apply your filter
imagefilter($im, IMG_FILTER_GRAYSCALE);
// Save your changes
imagepng($im, $tempFile);
move_uploaded_file($tempFile,$targetFile);
echo "1";
}
To use imagefilter you have to load image first. Use one of GD load functions ( like: imagecreatefrompng).
Then you can use filter on loaded image. By the way check parameters for imagefilter (which requires loaded image, not path to image). Here is some example code (that replaces your imagefilter()):
// Check extension of the file, here is example if the file is png, but you have to check for extension and use specified function
$img = imagecreatefrompng($tempFile);
if( imagefilter($img, IMG_FILTER_GRAYSCALE) )
{
// success
}
else
{
// failture
}
// Save file as png to $targetFile
imagepng($img, $targetFile);
// Destroy useless resource
imagedestroy($img);
what would be basic code for masking one image with another in GD - one image with black shape and transparent background would be used to crop another image - a photo so that photo is in the shape of black image.
One way to do it is using phpThumb.
Basic reference here: http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php#x31
If creating the new image on the fly it would be something as simple as:
<img src="../phpThumb.php?src=path/to/image/image.jp&fltr[]=mask|path/to/mask/mask.png&f=png" alt="">
To output into a png.
If doing this after an image upload to create a new image to be stored on the server, first figure out the basics of phpThumb and then set the mask parameters with all the rest:
For example:
...
require_once('phpThumb/phpthumb.class.php');
//Begin phpThumb work to resize image and create thumbnail
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . $destination;
$uploadfile = $uploaddir . $file;
$phpThumb = new phpThumb();
// set data source -- do this first, any settings must be made AFTER this call
$phpThumb->setSourceFilename($uploadfile);
$phpThumb->setParameter('w', 360); //change to update the picture size
$phpThumb->setParameter('h', 470); //change to update the picture size
$phpThumb->setParameter('fltr[]', 'mask|path/to/mask/mask.png'); //set mask
$phpThumb->setParameter('f', 'png'); //set png output format
$outputdir = $_SERVER['DOCUMENT_ROOT'] . $destination;
$output_filename = $outputdir . "masked" . $file;
$phpThumb->setParameter('config_allow_src_above_docroot', true);
if ($phpThumb->GenerateThumbnail()) { // this line is VERY important, do not remove it!
if ($phpThumb->RenderToFile($output_filename)) {
...