Two image not create thumbnail at same time in Php - php

i am trying to upload two images ("image" and "coverimage") at same time with thumbnail but second code/"image"
not creating thumbnail and not uploading to folder,First Code/"coverimage" working properly but "image" not creating thumbnail
Where i am wrong ? Here is my code
//uploading image ( working fine and creating thumbnail)
if( isset( $_FILES['CoverImage'] ) ){
if (file_exists($_FILES['CoverImage']['tmp_name']) || is_uploaded_file($_FILES['CoverImage']['tmp_name']))
{
$filenames = time() . uniqid(rand()) . $_FILES['CoverImage']['name'];
move_uploaded_file($_FILES["CoverImage"]["tmp_name"], "ShopCoverImage/" . $filenames);
$source_paths = $_SERVER['DOCUMENT_ROOT'] . '/ShopCoverImage/' . $filenames;
$target_paths = $_SERVER['DOCUMENT_ROOT'] . '/ShopCoverImage/thumb/';
$config_manips = array(
'image_library' => 'gd2',
'source_image' => $source_paths,
'new_image' => $target_paths,
'maintain_ratio' => FALSE,
'create_thumb' => TRUE,
'thumb_marker' => '',
'width' => 100,
'height' => 100
);
$this->load->library('image_lib', $config_manips);
if (!$this->image_lib->resize()) {
$error= $this->image_lib->display_errors();
return $error;
}
else
{
$saveArr['CoverImage'] = $filenames;
}
$this->image_lib->clear();
}
}
//image uploading ( not creating thumbnail)
if( isset( $_FILES['image'] ) ){
if (file_exists($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name']))
{
$filename = time();
$filename = time() . uniqid(rand()) . $_FILES['image']['name'];
move_uploaded_file($_FILES["image"]["tmp_name"], "Profile/" . $filename);
$source_path = $_SERVER['DOCUMENT_ROOT'] . '/Profile/' . $filename;
$target_path = $_SERVER['DOCUMENT_ROOT'] . '/Profile/thumb/';
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $source_path,
'new_image' => $target_path,
'maintain_ratio' => FALSE,
'create_thumb' => TRUE,
'thumb_marker' => '',
'width' => 100,
'height' => 100
);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
$error= $this->image_lib->display_errors();
return $error;
}
else
{
//$saveArr['image'] = $filename;
}
$this->image_lib->clear();
}
}

Please use below code :
Remove from second upload image code
$this->load->library('image_lib', $config_manip);
and replace with below code
$this->image_lib->initialize($config_manip);

Related

Image not resize and upload in Codeigniter PHP

I am trying to create thumbnail(resize) of uploaded image using codeigniter,
But image is not uploaded in server/folder,Here is my code, Where I went wrong ?
if (isset($_FILES['image'])) {
if (file_exists($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name'])) {
$filename = time() . uniqid(rand()) . $_FILES['image']['name'];
move_uploaded_file($_FILES["image"]["tmp_name"], "vendorProfile/" . $filename);
$saveArr['image'] = $filename;
$this->load->library('image_lib');
$source_paths = base_url() . 'vendorProfile/' . $filename;
$source_path = $_SERVER['DOCUMENT_ROOT'] . '/Profile/vendorProfile/' . $filename;
$target_path = $_SERVER['DOCUMENT_ROOT'] . '/Profile/vendorProfile/thumb';
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $source_path,
'new_image' => $target_path,
'maintain_ratio' => TRUE,
'create_thumb' => TRUE,
'thumb_marker' => '_thumb',
'width' => 150,
'height' => 150
);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
} else {
echo "image is uploaded";
}
$this->image_lib->clear();
}
}
You just look at the file path which you pass on $config_manip array where you need to pass file path without Http or https PHP don't accept Http. So if in your case your file struct like something this
-
application
system
image_folder <----
Then you just pass like something this
$config_manip = array(
'image_library' => 'gd2',
'source_image' => './image_folder/'.$file_name,
'new_image' => './thumb_folder/'.$target_path,
'maintain_ratio' => TRUE,
'create_thumb' => TRUE,
'thumb_marker' => '_thumb',
'width' => 150,
'height' => 150
);
If you are using the ubuntu server you need to give permission for that folder to upload the file/image.
if (!file_exists($target_path)) {
if (!mkdir($target_path, 0777, true)) {
chmod($target_path, 0777);
}
}

Warning: file_put_contents(): Filename cannot be empty in url/xyz.php on line xyz

Here i am trying to expose the custom web services of wordpress website.
In my website there is section where user can upload their Resume and Cover letter message.
I am facing below error:
Warning: file_put_contents(): Filename cannot be empty in
C:\xampp\htdocs\project1\webservices\candidate.php on line 255
{"result":"failed","message":"File upload failed"}
I created one function for this :
function save_candidate_cvcover($user_id,$cv,$cover_latter)
{
if(!isset($cv))
{
$ar = array ("result" => "failed", "message" => "No present");
echo json_encode($ar);
exit();
}
if(!isset($ext))
{
$ar = array ("result" => "failed", "message" => "No extension present");
echo json_encode($ar);
exit();
}
$base_dir = dirname(dirname(__FILE__));
$dir = wp_upload_dir();
$image=$cv;
$images=explode(",", $image);
$data = base64_decode($images[1]);
$ext = str_replace(".","",$ext);
$fileName= time().'_'.uniqid() . ".".$ext;
$file = $dir['path'].'/'.$fileName;
$success = file_put_contents($file, $data);
$image = wp_get_image_editor($file);
$sizes_array = array(
array('width' => 270, 'height' => 203, 'crop' => true),
array('width' => 236, 'height' => 168, 'crop' => true),
array('width' => 200, 'height' => 200, 'crop' => true),
array('width' => 180, 'height' => 135, 'crop' => true),
array('width' => 150, 'height' => 113, 'crop' => true),
);
$resize = $image->multi_resize($sizes_array, true);
$img_resized_name = isset($resize[0]['file']) ? basename($resize[0]['file']) : '';
$filename = $img_resized_name;
$filetype = wp_check_filetype(basename($filename), null);
if ($filename != '') {
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . ($filename),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', ($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the attachment.
$attach_id = wp_insert_attachment($attachment, $filename);
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata($attach_id, $filename);
wp_update_attachment_metadata($attach_id, $attach_data);
}
add_user_list_ment($user_id,'cs_candidate_cv', $wp_upload_dir['url'] . '/' . $file);
add_user_list_ment($user_id,'cs_cover_letter', $cover_latter);
$ar = array ("result" => "success", "message" => "CV & Cover letter updated successfully");
echo json_encode($ar);
exit();}
Below is the code i am using while calling the method:
if($method=='get_candidate_cvcover')
{
$user_id=esc_sql($jsonPOST->user_id);
get_candidate_cvcover($user_id);
}
/*Function for get candidate profile*/
if($method=='save_candidate_cvcover')
{
$user_id=esc_sql($jsonPOST->user_id);
$fileName= time().'_'.uniqid() . ".".$ext;
$wp_upload_dir = wp_upload_dir();
$success = file_put_contents($file, $data);
$name = $_FILES["cv"]["name"];
$ext = end((explode(".", $name))); # extra () to prevent notice
$file = $wp_upload_dir['path'].'/'.$name.'.'.$ext;
if (move_uploaded_file($_FILES["cv"]["tmp_name"], $file))
$file = $name.'.'.$ext;
else{
$ar = array ("result" => "failed", "message" => "File upload failed");
echo json_encode($ar);
exit();
}
$cover_latter=esc_sql($jsonPOST->cover_latter);
save_candidate_cvcover($user_id,$file,$cover_latter);
}
$cache_disqus is defined outside your function, and is therefore not accessible within the function.
Check out the PHP documentation on variable scope.

codeigniter resize image and create thumbnail

hi according to the ci document you can resize images with image_lib and there are options that suggest we can create additional thumbnail from that image
create_thumb FALSE TRUE/FALSE (boolean) Tells the image processing function to create a thumb. R
thumb_marker _thumb None Specifies the thumbnail indicator. It will be inserted just before the file extension, so mypic.jpg would become mypic_thumb.jpg R
so here is my code
$config_manip = array(
'image_library' => 'gd2',
'source_image' => "./uploads/avatar/tmp/{$this->input->post('new_val')}",
'new_image' => "./uploads/avatar/{$this->input->post('new_val')}",
'maintain_ratio'=> TRUE ,
'create_thumb' => TRUE ,
'thumb_marker' => '_thumb' ,
'width' => 150,
'height' => 150
);
$this->load->library('image_lib', $config_manip);
$this->image_lib->resize();
i would assume this code resizes my image and also creates a thumbnail , but i only get one image with specified dimensions and _tump postfix
i've also tried to add this code to create second image manually but still it doesn't work and i get only one image
$this->image_lib->clear();
$config_manip['new_image'] =
"./uploads/avatar/thumbnail_{$this->input->post('new_val')}";
$config_manip['width'] = 30 ;
$config_manip['height'] = 30 ;
$this->load->library('image_lib', $config_manip);
$this->image_lib->resize();
It seems path is the issue in your code. I modified and tested myself it works.
public function do_resize()
{
$filename = $this->input->post('new_val');
$source_path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/avatar/tmp/' . $filename;
$target_path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/avatar/';
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $source_path,
'new_image' => $target_path,
'maintain_ratio' => TRUE,
'create_thumb' => TRUE,
'thumb_marker' => '_thumb',
'width' => 150,
'height' => 150
);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
// clear //
$this->image_lib->clear();
}
Hope this helps you. Thanks!!
Your code is Okay but you need to do a small change.
$this->load->library('image_lib');
$this->image_lib->initialize($config_manip);
A simple way to create a thumbnail.
function _create_thumbnail($fileName, $width, $height)
{
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = $_SERVER['DOCUMENT_ROOT']. $fileName;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$config['new_image'] = $_SERVER['DOCUMENT_ROOT']. $fileName;
$this->image_lib->initialize($config);
if (! $this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
}
If you want to create more than one image using resize() method, you need to call $this->image_lib->initialize($config); each time you attempt a resize.
This tutorial solved it for me Upload Image and Create Multiple Thumbnail Sizes in CodeIgniter

Generating image thumb in codeigniter

I am pretty new to codeigniter and just building my first application with it, but I am abit stumped when it comes to generating a thumbnail from an image.
The image uploads correctly but the thumb isnt generating and I am getting no errors :(
I hope someone can give me a helping hand, chances are I am just being a tit and its something really simple like mis spelling var.
Heres the code for my image model:
<?php
class Image_model extends CI_Model {
var $image_path;
function Image_model(){
parent::__construct();
$this->image_path = realpath(APPPATH.'../'.$this->config->item('dir_dynamic_images'));
}
function do_upload(){
$config = array(
'allowed_types' => "jpeg|gif|jpg|png",
'upload_path' => $this->image_path,
'max_size' => 2000
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->image_path . '/thumbs',
'maintain_ratio' => true,
'width' => 200,
'height' => 200
);
echo $config['new_image'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
?>
I think you need to set the create_thumb parameter to true and specify the image library-
$config = array(
'image_library' => 'gd2',
'source_image' => $image_data['full_path'],
'create_thumb' => true,
'new_image' => $this->image_path . '/thumbs',
'maintain_ratio' => true,
'width' => 200,
'height' => 200
);
try finding the error with -
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
This piece of code is working for me. Hopefully it will works for you . you can also check the image manipulation helper in codeigniter. Don'f forget to initialize the config.
$config['image_library'] = 'imagemagick';
$config['library_path'] = '/usr/X11R6/bin/';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['x_axis'] = '100';
$config['y_axis'] = '60';
$this->image_lib->initialize($config);
if ( ! $this->image_lib->crop())
{
echo $this->image_lib->display_errors();
}

PHP/Codeigniter - For Loop [Undefined offset]

for($i=0;$i<count($status);$i++)
{
$conf = array(
'source_image' => $status[$i]['full_path'],
'new_image' => $this->upload_path . '/thumbs',
'maintain_ratio' => true,
'width' => 200,
'height' => 200
);
$this->load->library('image_lib', $conf);
$this->image_lib->resize();
$this->image_lib->clear(); // complete reset
$this->image_lib->initialize($conf); // complete reset
}
.
always keep missing the last thumbnail creation cycle. when trying for($i=0;$i<=count($status);$i++). i get this notice Undefined offset
By using a for loop you are assuming that the keys of the array are contiguous, which they may not be. You are also assuming that every second level array has a full_path key, which it may not. Use foreach instead, and do an isset() check on the full_path key:
foreach ($status as $item)
{
if (!isset($item['full_path'])) continue;
$conf = array(
'source_image' => $item['full_path'],
'new_image' => $this->upload_path . '/thumbs',
'maintain_ratio' => true,
'width' => 200,
'height' => 200
);
$this->load->library('image_lib', $conf);
$this->image_lib->resize();
$this->image_lib->clear(); // complete reset
$this->image_lib->initialize($conf); // complete reset
}
try this:
$this->load->library('image_lib');
$stat = array_values($status);
for($i=0;$i<count($stat);$i++)
{
$conf = array(
'source_image' => $stat[$i]['full_path'],
'new_image' => $this->upload_path . '/thumbs',
'maintain_ratio' => true,
'width' => 200,
'height' => 200
);
$this->image_lib->initialize($conf); // complete reset
$this->image_lib->resize();
}

Categories