WordPress Custom Image Upload Box

I am developing a custom field for uploading images in WordPress, but I have a hard time having an image problem after uploading it. In addition to uploading, I need to resize the image to use thumbnail. Every time I try to work with the downloaded image, I get an error when you cannot find the file (although I can view it in the browser, and it is clearly located in the directory). When loading images, the default is 666, but I also tried to manipulate them on 777 with the same results. The resize function is called independently after loading the image. Here is one of my attempts:

function resize_author_picture($filename) {
  $filename = $_POST['file'];
  $file = fopen($filename, 'r');
  $data = fread($file);
  fclose($file);
  $dimensions = getimagesize($filename);
  $dir = dirname($filename);
  $crop = wp_crop_image( $data, $dimensions[0], $dimensions[1], 0, 0, 250, 280, null, $dir."/image.jpg" );

  die( "crop: ".var_dump($crop)." file: ".$filename." path: ".$dir."/image.jpg" );
 }

Here I used fopen () as a second attempt when providing only the image path did not work. Here is the previous attempt:

function resize_author_picture($file) {
 $file = $_POST['file'];
 $dimensions = getimagesize($file);
 $dir = dirname($file);
 $crop = wp_crop_image( $file, $dimensions[0], $dimensions[1], 0, 0, 250, 280, null, $dir."/image.jpg" );
 die( "crop: ".var_dump($crop)." file: ".$file." path: ".$dir."/image.jpg" );
}

Both return a WP error object on these lines:

string(123) "File <http://site.local/wp-content/uploads/2010/09/squares-wide.jpeg> doesn't exist?"

Running ideas, any input appreciated!

+3
source share
2 answers

I doubt that you still need an answer, since this question is quite old, but here it is for reference in the future:

The error you received is the return wp_load_imagethat is being used wp_crop_image. wp_load_imageuses the php function file_exists, which must be passed the file path without a domain.

So,

$crop = wp_crop_image( $file, $dimensions[0], $dimensions[1], 0, 0, 250, 280,
        null, "wp-content/uploads/2010/09/squares-wide.jpeg" );

would work.

Aside, wp_upload_bitsnot only the file is uploaded for you, it also returns the URL of the downloaded file.

If you call the wp_upload_bitssame way (where "file" is the name of the form input):

if ($_FILES["file"]["name"]!="") {
    $uploaded_file = wp_upload_bits($_FILES["file"]["name"], null,
        file_get_contents($_FILES["file"]["tmp_name"]));
}

, $uploaded_file['url'] $dir."/image.jpg". $uploaded_file['url'].

:

http://site.local/wp-content/uploads/2010/09/squares-wide.jpeg :

$dir = dirname($file);
$dir_substr = substr($dir, 18)
$crop = wp_crop_image( $file, $dimensions[0], $dimensions[1], 0, 0, 250, 280,
        null, $dir."/squares-wide.jpeg" );

, , , wp_uload_bits, ( , WP Custom , , part $uploaded_file = wp_upload_bits(...), wp_upload_bits ),

$file_uri = substr($uploaded_file['url'], 18);
$crop = wp_crop_image( $file, $dimensions[0], $dimensions[1], 0, 0, 250, 280,
        null, $file_uri );
+2

, /wp -content/uploads, admin.

, .

WP Post, , . - .

0

Source: https://habr.com/ru/post/1766435/


All Articles