How can I resize my image? - php

This is my image uploading and resizing code in codeigniter.But in my client's server the image is uploaded but not create a resized image into a folder
if($imgwidth >= 1025 && $imgheight >= 650)
{
$epld=explode('.',$ex);
$filename=date("mdyHis").".".$epld[1];
$uploaddir = './bg_images/';
$file = $uploaddir . basename($filename);
if(move_uploaded_file($_FILES['file']['tmp_name'],"./bg_images/".$filename))
{
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = $file ;
$config['maintain_ratio'] = FALSE;
$this->image_lib->initialize($config);
$this->md_image->resize_image('./bg_images/'.$filename,1025,650,'./bg_images/thumbs/'.$filename);
}
}
The bg_images is the folder name and the md_image is the library used for resizing

I can not spot any errors in your code.
Make sure you have gdlib installed by uploading a script with the following content:
<?php
echo phpinfo();
?>
The Script shows your php configuration.
Search for "gd" with your browser's search function. No mention of "gd" would mean that you are missing this library, but you need it for image manipulation.

does the ./bg_images/thumbs/ folder exist on the file system?
As far as I remember, gdlib would not create folder itself. If the folder is missing, create it yourself (remember to give it the right permissions, the server / php process needs write permission (most likely www-data))
If all this doesn't help, ask your client's admin for access to the error logs.

Related

Codeigniter how to upload the resize image or thumb ONLY

I am new with CodeIgniter and want to resize the image first before saving to my database and local storage using move_uploaded_file i was able to resize using below code but how to I access the resized image and upload it using move_uploaded_file on PHP
$uploaddir = './images/post/';
$uploadfile = $uploaddir . basename($value1."-".$date."-".$_FILES['file']['name']);
$config['image_library'] = 'gd2';
$config['source_image'] = $uploadfile;
$config['create_thumb'] = TRUE;
$config['width'] = 350;
$config['height'] = 1;
$config['maintain_ratio'] = TRUE;
$config['master_dim'] = 'width';
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
Any suggestion would be great.
You should understand that essentially what you are doing is uploading the file to the temporary directory and using the locally stored file to generate a resized image.
So for better or worse your image is already on your server. Thus:
Your order of operations is wrong. You need to use the upload library to first bring the image on to your server, and then you can use that file location instead of the temporary name, to resize the image. With Codeigniters image lib you can make it so that the resized image overwrites the original. To do so you change $config['create_thumb'] = false; and resize will target the source/original image.
More on that setting here: https://www.codeigniter.com/userguide3/libraries/image_lib.html#CI_Image_lib::resize
If you really want to go your route (which isn't great because you don't have the protections the upload library offers) turn $config['create_thumb'] to false as I've said before OR note that the thumb file should be the temp name + _thumb + whatever extension.
See thumb marker here: https://www.codeigniter.com/userguide3/libraries/image_lib.html#preferences
When user submits a file(form must have enctype="multipart/form-data" attribute for this to work):
file is being uploaded to configured server directory upload_tmp_dir
uploaded file properties are stored in $_FILE array: original name, mime type, temporary name (documentation)
after script ends temporary file is being deleted
So, if you want to store only resized image, you can process directly temporary file using absolute path in $_FILES['file']['tmp_name'] property.

Codeigniter resize issue after transferring server

The code was working like a charm for me. But I have now transferred the files from one server to another, and it is not working now. The image is being uploaded (original) -> image.jpg but the other two images after resize thumb_image.jpg and featured_image.jpg are not being uploaded. I dont know what the problem is.
I went through the error log and I see these 3 lines of codes
ERROR - 2013-09-08 17:38:26 --> PNG images are not supported.
ERROR - 2013-09-08 17:38:26 --> The path to the image is not correct.
ERROR - 2013-09-08 17:38:26 --> Your server does not support the GD function required to process this type of image.
I dont understand whats the issue with the image path and why does it says PNG images are not supported as it was working perfectly fine.
The resize code is
public function resizeIMG($imagePath, $filename){
$this->load->library('image_lib');
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = $imagePath;
$configThumb['create_thumb'] = FALSE;
$configThumb['new_image'] = 'thumb_'.$filename;
$configThumb['maintain_ratio'] = TRUE;
$configThumb['width'] = 260;
$configThumb['height'] = 215;
$configFeatured['image_library'] = 'gd2';
$configFeatured['source_image'] = $imagePath;
$configFeatured['create_thumb'] = FALSE;
$configFeatured['new_image'] = 'featured_'.$filename;
$configFeatured['maintain_ratio'] = TRUE;
$configFeatured['width'] = 800;
$configFeatured['height'] = 500;
$configCropFeatured['image_library'] = 'gd2';
$configCropFeatured['source_image'] = './uploads/featured_'.$filename;
$configCropFeatured['x_axis'] = '0';
$configCropFeatured['y_axis'] = '0';
$configCropFeatured['create_thumb'] = FALSE;
$configCropFeatured['new_image'] = 'featured_'.$filename;
$configCropFeatured['maintain_ratio'] = FALSE;
$configCropFeatured['width'] = 720;
$configCropFeatured['height'] = 250;
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
$this->image_lib->initialize($configFeatured);
$this->image_lib->resize();
$this->image_lib->initialize($configCropFeatured);
$this->image_lib->crop();
}
The logs say Your server does not support the GD function required to process this type of image. which means that your server host has not enabled the GD library (or possibly banned the functions you are using for whatever reason)
You will need to contact your webhost to see if they can enable GD. If not, you will need to convert your application to use a different library. You can find out which libraries are available by running phpinfo() inside a PHP script. If GD is disabled, it's most likely that instead, ImageMagick is turned on - you can check out that page and you will be able to convert your application to use ImageMagick once you understand the basic functions. Please make sure that ImageMagick (or imagick) is somewhere in phpinfo(). If not, you're probably best asking your host to enable either of the plugins.

CodeIgniter - Convert GIF to JPG? Possible?

I am looking to accomplish this in CodeIgniter specifically.
The PHP App I am coding allows a user to upload either a jpg or an animated gif image. On the next step I want to allow the user to use jCrop to crop a few different size thumbnails. This would require me to convert a new copy of the animated gif to a jpg. My code works fine with uploaded jpg images, but creates a broken image for gif files. Is what I am trying to do possible?
My Code:
// Create image to crop
$config['image_library'] = 'ImageMagick';
$config['library_path'] = '/usr/bin';
$config['source_image'] = $this->config->item('upload_dir_path') . $file_path . 'original.' . $file_ext;
chmod($config['source_image'], 0777);
$config['new_image'] = $this->config->item('upload_dir_path') . $file_path . 'crop-me.jpg';
$this->image_lib->initialize($config);
$this->image_lib->resize();
For those interested in my solution, I simply used the built in GD PHP functions. I have little experience dealing with gif files so I was expecting this to be difficult. The fact of the matter is the CodeIgniter Image_lib and extended library (Which I never got to work properly) is overkill for this.
Here is what I used:
$image = imagecreatefromgif($path_to_gif_image);
imagejpeg($image, $output_path_with_jpg_extension);
Very easy and worked perfectly for what I needed.

Thumbnail image of PDF using ImageMagick and PHP

After searching Google and SO, I found this little bit of code for creating thumbnails of PDF documents using ImageMagick.
The trouble for me is in implementing it into my WordPress theme. I think that I'm getting stuck on the path to cache that the script needs for temporary files.
I'm using it as described in the article:
<img src="http://localhost/multi/wp-content/themes/WPalchemy-theme/thumbPdf.php?pdf=http://localhost/multi/wp-content/uploads/2012/03/sample.pdf&size=200 />
which must be right (maybe... but I assume i am correct to use full URL to the actual file), because when I click on that URL I am taken to a page that reads the following error:
Unable to read the file: tmp/http://localhost/multi/wp-content/uploads/2012/03/sample.pdf.png
Now tmp is defined in the thumbPdf.php script, but I am confused as to what it's value should be. Is it a url or a path? Like timthumb.php, can i make it be relative to the thumbPdf.php script? (I tried ./cache which is the setting in timthumb -and was sure to have a /cache folder in my theme root, to no avail). also, fyi I put a /tmp folder in my root and still get the same error.
So how do I configure tmp to make this work?
http://stormwarestudios.com/articles/leverage-php-imagemagick-create-pdf-thumbnails/
function thumbPdf($pdf, $width)
{
try
{
$tmp = 'tmp';
$format = "png";
$source = $pdf.'[0]';
$dest = "$tmp/$pdf.$format";
if (!file_exists($dest))
{
$exec = "convert -scale $width $source $dest";
exec($exec);
}
$im = new Imagick($dest);
header("Content-Type:".$im->getFormat());
echo $im;
}
catch(Exception $e)
{
echo $e->getMessage();
}
}
$file = $_GET['pdf'];
$size = $_GET['size'];
if ($file && $size)
{
thumbPdf($file, $size);
}
I have seen this answer:
How do I convert a PDF document to a preview image in PHP?
and am about to go try it next
The error tells everything you need.
Unable to read the file: tmp/http://localhost/multi/wp-content/uploads/2012/03/sample.pdf.png
Script currently tries to read file from servers tmp/ folder.
$tmp = 'tmp';
$format = "png";
$source = $pdf.'[0]';
//$dest = "$tmp/$pdf.$format";
$dest = "$pdf.$format";
Remember securitywise this doesn't really look so good, someone could exploit ImageMagic bug to achieve very nasty things by giving your script malformed external source pdf. You should at least check if the image is from allowed source like request originates from the same host.
Best way to work with ImageMagic is to always save the generated image and only generate a new image if generated image doesn't exist. Some ImageMagic operations are quite heavy on large files so you don't want to burden your server.

File permission to PHP uploaded image

In my PHP site I have a script to upload images, see the script below
$uploaddir = $root_path."images/uploaded_images/category/";
$small_file_name = trim($_FILES['cat_image1']['name']);
$small_file_len = strlen($small_file_name);
$small_file_ext = strtolower(substr($small_file_name,-4)); // select last 4 characters
$small_uploadfile = $uploaddir. $_FILES['cat_image1']['name'];
if($small_file_len>4 and ($small_file_ext==".gif" or $small_file_ext==".jpg" or $small_file_ext=="jpeg")){
if ($small_file_ext=="jpeg")
$uniqname = uniqid(rand()).".".$small_file_ext;
else
$uniqname = uniqid(rand()).$small_file_ext;
$thumb_filename1 = "thumb_".$uniqname; // store uniqname into database
$uploadfile = $uploaddir.$uniqname; //uncomment for local testing
if (move_uploaded_file($_FILES['cat_image1']['tmp_name'], $uploadfile)) {
chmod($uploadfile,0777);
list($width, $height, $type, $attr) = getimagesize($uploadfile);
$max_width = 276;
$max_height = 162;
$a = new Thumbnail($uploadfile,$max_width,$max_height,$uploaddir.$thumb_filename1,100,'');
$a->create();
}else{
$flag=1;
$frm_server_side_error= $frm_server_side_error."Error in uploading image,";
}
}
else{
$flag=2;
$frm_server_side_error= $frm_server_side_error."Image not in gif or jpg format,";
}
After uploading in to the server its can't be see (I am displaying the thumb image). So I checked the file permission of that image through the FTP and give read permission. Thus image displays. But I give the read and write permission to the category folder and it’s parent folders. How can view the image after uploading (default read permission to the uploading files ie thumb image.)
I noticed that the actual image have read permission but the thumb image doesn't have the read permission. I need to display only the thumb image.
You have used a class Thumbnail to create a thumbnail image. But I guess you have missed to change the file permission in the new thumbnail image. Please check the code in the create() function of the class Thumbnail
Your code formatted poorly here (half is in code format, half not), but it appears you have a comment before the snippet that would copy the file from the temp location to the final location - if that's the case, the file won't exist, and thus won't be viewable.
But I could be wrong - I'm happy to take a second look if you fix the formatting.

Categories