I tried two methods for uploading blob files to WordPress media library.
In first method i tried move blob tmp file to WordPress upload directory with move_uploaded_file(). Then tried upload this temp file with media_handle_sideload() function.
It is blob file that i sent via ajax:
array(
'name' => '2021-03-26_02-58-09',
'type' => 'image/png',
'tmp_name' => 'C:\Users\me\AppData\Local\Temp\php28D8.tmp',
'error' => 0,
'size' => 1880058,
);
Ajax process:
$file = $_FILES['file'];
$upload_dir = wp_upload_dir();
move_uploaded_file( $file['tmp_name'], $upload_dir['path'] . '/' . $file['name'] );
$url = $upload_dir['url'] . '/' . $file['name'];
$tmp = download_url( $url );
$file_array = array(
'name' => basename( $url ),
'tmp_name' => $tmp,
);
$id = media_handle_sideload( $file_array, 0 );
var_dump( $id );
But it returns Sorry, this file type is not permitted for security reasons error.
So i tried this with wp_insert_attachment() function. It is uploading file but without image extension (.jpg, .png) and it is not generating additional image sizes. Here is what i tried:
$file = $_FILES['file'];
$upload_dir = wp_upload_dir();
move_uploaded_file( $file['tmp_name'], $upload_dir['path'] . '/' . $file['name'] );
$id = wp_insert_attachment(
array(
'guid' => $upload_dir['path'] . '/' . $file['name'],
'post_mime_type' => $file['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', $file['name'] ),
'post_content' => '',
'post_status' => 'inherit',
),
$upload_dir['path'] . '/' . $file['name']
);
var_dump( $id );
Related
I am having an issue with DOMPDF image loading. I tried all the way to load the image but it won't work on my local / server.
isRemoteEnabled mode changed to true.
I wanted to load a PNG image.
Please help me if you have an idea of how to resolve this issue.
I really appreciate any help you can provide.
When i try this code am getting mem
Constructor:
$options = new Options();
$options->set('defaultFont', 'Helvetica');
$options->set('isRemoteEnabled', TRUE);
$options->set('debugKeepTemp', true);
$options->set('isHtml5ParserEnabled', true);
$this->dompdf = new Dompdf($options);
Fucntion :
$path = '../logo.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
#data
$data = array_merge($this->data, [
'title' => $this->title,
'word' => $this->word,
'breadcrumb' => array("0" => array("url" => base_url("home"), "title" => 'Home'), "1" => array("url" => $url,
"title" => $this->word_multi)),
'action' => $url,
'customerID' => $cid,
'siteID' => $site,
'contractRow' => $contractRow,
'base64' => $base64
]);
#Contract file name
$contract = str_replace('/', "-", $contractRow['contractNumber']);
#remove UTF8 issue header
$this->response->removeHeader('Content-Type');
#viewpage data
$this->dompdf->loadHtml(view($this->path_views . 'contract', $data));
$this->dompdf->set_option('defaultMediaType', 'all');
$this->dompdf->setPaper('A4','portrait');
$this->dompdf->render();
$this->dompdf->stream( $contract, array("Attachment" => false));
View
<td width="20%"> <img src="<?php echo $base64; ?>" /></td>
I have searched far and wide, but have not found a solution.
I have a real estate ad insertion form, I need to be able to upload photos of the listings. I can upload the photos to the server via ajax, return the filenames in a textarea and always via ajax, after submitting the form, I can upload the photos to wordpress and attach them to the ad
The only problem is that it does not generate the photo metadata, wp_generate_attachment_metadata always returns an empty array.
I can't find a solution about it. I have another plugin with a similar form, but there I post the form not via ajax, but with the action = "post", and I can safely generate the metadata.
This is the code with which I insert the attachments and link them to the newly created post.
Hope someone can help me
//$filename = domoria-torino-strada-della-fornace-druento-15.jpg
if ($filename != '') {
$wp_upload_dir = wp_upload_dir();
$filename_path = $wp_upload_dir['path'] .'/'. $filename;
$filename_url = $wp_upload_dir['url'] .'/'. $filename;
$guid = $wp_upload_dir['url'] . '/' . basename( $filename_path );
$attachment = array(
'guid'=> $guid,
'post_mime_type' => 'image/jpeg',
'post_title' => $filename,
'post_content' => '',
'post_status' => 'inherit',
'post_parent' => $post_id
);
$attach_id = wp_insert_attachment( $attachment, $filename_path);
if($iter === 0){
set_post_thumbnail( $post_id, $attach_id );
}
$ids [] = $attach_id; //this array needs for an ACF field
//filename_path = home/uxo80ef6/domains/homeprime.sviluppo.host/public_html/wp-content/uploads/2021/12/domoria-torino-strada-della-fornace-druento-15.jpg
//$attach_id = 629
$file_uploaded_path = get_attached_file($attach_id);
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $file_uploaded_path );
wp_update_attachment_metadata( $attach_id, $attach_data );
$iter++;
}
UPDATE: The problem is due to getimagesize called by wp_generate_attachment_metadata that can't find the file by file_path, however the file is on the server.
I rewrote some parts of the code you posted:
change hardcoded post_mime_type with wp_check_filetype() instead.
rename some of the variables to: $file_name, $file_path, $parent_post_id
removed unused $filename_url
added $parent_post_id as the third argument into wp_insert_attachment()
removed get_attached_file() function and used $file_path for wp_generate_attachment_metadata()
<?php
// $file_name = domoria-torino-strada-della-fornace-druento-15.jpg
if (!empty($file_name)) {
// The ID of the post this attachment is for.
// eg. $parent_post_id = 37;
// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();
$file_path = $wp_upload_dir['path'] . '/' . $file_name;
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype(basename($file_path), null);
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename($file_path),
'post_mime_type' => $filetype['type'],
'post_title' => sanitize_title($file_name),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the attachment.
$attach_id = wp_insert_attachment($attachment, $file_path, $parent_post_id);
// Set the first attachment as Featured image.
if ($iter === 0) {
set_post_thumbnail($parent_post_id, $attach_id);
}
// ACF field array data.
$ids[] = $attach_id;
// 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, $file_path);
wp_update_attachment_metadata($attach_id, $attach_data);
$iter++;
}
Hello guys i'm a little bit confuse with my mpdf function in codeigniter to generate multiple file pdf, i tried to research and nothing can solve this is issue,
i have anchor that action to download function, and the idea is to call all function with mpdf generate in it. here is the code :
public function download($id)
{
$this->kehadiranDosen($id);
$this->nilaiSeminar($id);
$this->beritaAcara($id);
}
function beritaAcara($id)
{
$data['peserta'] = $this->db->get_where('seminar', ['id' => $id])->row_array();
//Berita Acara
$html = $this->load->view('seminar/beritaAcara', $data, true);
$this->load->library('M_pdf');
$mpdf = $this->m_pdf->load([
'mode' => 'utf-8',
'format' => 'A4'
]);
$mpdf->WriteHTML($html);
$mpdf->Output('BERITA ACARA,' . ucfirst($data['peserta']['nama']) . ',' . $data['peserta']['npm'] . '.pdf', "I");
unset($mpdf);
unset($html);
}
function kehadiranDosen($id)
{
$data['peserta'] = $this->db->get_where('seminar', ['id' => $id])->row_array();
//Kehadiran Dosen
$html2 = $this->load->view('seminar/kehadiranDosen', $data, true);
$this->load->library('M_pdf');
$mpdf2 = $this->m_pdf->load([
'mode' => 'utf-8',
'format' => 'A4'
]);
$mpdf2->WriteHTML($html2);
$mpdf2->Output('KEHADIRAN DOSEN,' . ucfirst($data['peserta']['nama']) . ',' . $data['peserta']['npm'] . '.pdf', "I");
unset($mpdf2);
unset($html2);
}
function nilaiSeminar($id)
{
$data['peserta'] = $this->db->get_where('seminar', ['id' => $id])->row_array();
//Nilai Seminar
$html3 = $this->load->view('seminar/nilaiSeminar', $data, true);
$this->load->library('M_pdf');
$mpdf3 = $this->m_pdf->load([
'mode' => 'utf-8',
'format' => 'A4'
]);
$mpdf3->WriteHTML($html3);
$mpdf3->Output('NILAI SEMINAR,' . ucfirst($data['peserta']['nama']) . ',' . $data['peserta']['npm'] . '.pdf', "I");
unset($mpdf3);
unset($html3);
}
and when i click it, only kehadiran dosen generated, anyone can solve this? thank you so much before
I'm running over this problem which I was trying for the last few hours
I'm having an image upload with some details to store in db.
I store the details and image path, working like a charm. Now comes the edit part.
I'm trying to check if the input file is empty, if so update just the details, else delete the image and reupload new image. The problem is this:
If the input file is empty it updates everything no problem, if is not empty it is updating the details, but the image is the same, doesn't get deleted or reuploaded.
here is the code
$image_input = $this->input->post('image');
if(isset($image_input) && !empty($image_input))
{
$this->db->where('id', $id);
$img = $this->db->get('menus_category', 1);
if($img->num_rows() > 0)
{
$row = $img->row();
$original_image = $row->image;
$desktop_image = $row->desk_img;
$mobile_image = $row->mob_img;
$thumb_image = $row->thumb;
$unlink_image = unlink('./uploads/menus/' . $original_image);
$unlink_desk = unlink('./uploads/menus/desk/' . $desktop_image);
$unlink_mob = unlink('./uploads/menus/mobile/' . $mobile_image);
$unlink_thumb = unlink('./uploads/menus/thumbs/' . $thumb_image);
if($unlink_desk && $unlink_image && $unlink_mob && $unlink_thumb)
{
$config = array(
'upload_path' => './uploads/menus',
'allowed_types' => 'gif|jpg|jpeg|png',
'max_size' => '15000'
);
$this->upload->initialize($config);
if ($this->upload->do_upload('image'))
{
$image_data = $this->upload->data();
$this->load->library('image_lib');
// thumb resize
$thumbnail = 'thumb_' . $image_data['file_name'];
$thumb = array(
'image_library' => 'GD2',
'source_image' => $image_data['full_path'],
'new_image' => $image_data['file_path'] . 'thumbs/' . $thumbnail,
'maintain_ratio' => TRUE,
'width' => '90',
'height' => '90'
);
$this->image_lib->initialize($thumb);
$this->image_lib->resize();
$this->image_lib->clear();
// mobile resize
$mob = 'mob_' . $image_data['file_name'];
$thumb_mob = array(
'image_library' => 'GD2',
'source_image' => $image_data['full_path'],
'new_image' => $image_data['file_path'] . 'mobile/' . $mob,
'maintain_ratio' => FALSE,
'width' => '290',
'height' => '83'
);
$this->image_lib->initialize($thumb_mob);
$this->image_lib->resize();
$this->image_lib->clear();
// desktop resize
$desk = 'desk_' . $image_data['file_name'];
$thumb_desk = array(
'image_library' => 'GD2',
'source_image' => $image_data['full_path'],
'new_image' => $image_data['file_path'] . 'desk/' . $desk,
'maintain_ratio' => FALSE,
'width' => '700',
'height' => '200'
);
$this->image_lib->initialize($thumb_desk);
$this->image_lib->resize();
$this->image_lib->clear();
// insert path and details to database
$data = array(
'title' => $input['title'],
'slug' => $this->_check_slug($input['title']),
'description' => $input['description'],
'image' => $image_data['file_name'],
'desk_img' => $desk,
'mob_img' => $mob,
'thumb' => $thumbnail
);
$this->db->where('id', $id);
return $this->db->update('menus_category', $data);
}
else
{
echo $this->image_lib->display_errors();
}
}
}
}
else
{
$data2 = array(
'title' => $input['title'],
'slug' => $this->_check_slug($input['slug']),
'description' => $input['description']
);
$this->db->where('id', $id);
return $this->db->update('menus_category', $data2);
}
Note: the else statement works fine, but the first if the problem. Now I changed the if to just if(isset($image_input)) ... and if file input is not empty is reuploading the picture and updating the details fine, but if I update only the details with no picture, it is deleting the picture that is already uploaded and is not updating. (I think this is the problem but I can't figure out how to fix it).
If you will to give me some help or to put me on right direction I will be thankful.
Thanks guys
First, this would be a comment asking a question, but I don't have enough reputation do add one just yet...
If you could post your HTML form, that would be helpful, but without that...
1) I assume the form you are submitting has enctype="multipart/form-data" in the opening tag.
2) On line 2 of your code here, $this->input->post('image'), is 'image' from a form input of type file? If so, your statement won't work as 'image' is part of the $_FILES array and not $_POST anymore. Meaning when you go to check it, it is always going to be empty (or non-existent).
To verify that your form is submitting the way you think it is, do this just before the first line in the code you have in your post:
var_dump($_POST);
var_dump($_FILES);
exit; // don't let anything run after the dumps.
let me know if that puts you in the right direction or not.
Just wondering if anyone can help with this - banging my head for days...
From a custom taxonomy page (taxonomy_genre.php), I need to output the child terms with images.
I'm using the 'Taxonomy Images' plugin to set the image.
Code below - thanks in advance if anyone can give me some pointers!
code #1 outputs all terms including parent terms.
code #2 outputs the correct terms but with no images
essentially I'm trying to amalgamate the two.
code #1:
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args = array(
'taxonomy' => $current_term->taxonomy,
'child_of' => $current_term->term_id,
'term_args' => array(
'orderby' => 'id',
'order' => 'ASC',
'hierarchical' => 0,
),
);
$cats = apply_filters( 'taxonomy-images-get-terms', '', $args );
foreach ($cats as $cat) {
echo '<li><a href="' . get_category_link($cat) . '" title="'. $cat->name .'">' ;
echo wp_get_attachment_image( $cat->image_id, 'detail' );
echo $cat->name ;
echo '</a></li>';
}
code #2
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$cats = wp_list_categories( array(
'child_of' => $current_term->term_id,
'taxonomy' => $current_term->taxonomy,
'hide_empty' => 0,
'hierarchical' => false,
'depth' => 2,
'title_li' => ''
));
foreach ((array)$cats as $cat) {
$catdesc = $cat->category_description;
echo '<li>'. wp_get_attachment_image( $cat->image_id, 'detail' ) . $cat->cat_name . '</li>'; }
Sorted - used:
'parent' => $current_term->term_id,
instead of:
child_of => $current_term->term_id,