0

I'm attempting to generate a thumbnail for an image I have posted, but it seems when I call image_downsize, the index[0] actually prints an array as the URL. I'm a bit confused, so any help would be appreciated!

My code:

$wpUploadPath = wp_upload_dir();
$target_path = $wpUploadPath;
$filename = 'http://www.mysite.org/uploads/sampleFile.jpeg';

$fileNameFinal = pathinfo($filename);

$wp_filetype = wp_check_filetype($filename, null);

$attachment = array(
 'post_mime_type' => $wp_filetype['type'],
 'post_title' => $fileNameFinal['filename'],
 'post_content' => '',
 'post_status' => 'inherit'
);

$attach_id = wp_insert_attachment( $attachment, $target_path);

$upFile = image_downsize( $attach_id, 'thumbnail' );

Here is where I encounter the problem. If I print_r($upFile); I get:

Array ( 
    [0] => http://www.mysite.org/uploads/Array
    [1] => 0 
    [2] => 0 
    [3] =>
) 

Is this normal? I can't seem to generate the thumbnail to update my user meta's with this.

Any help would be appreciated. Thanks!

Tre

2
  • What is the value of $attach_id? Feb 14, 2012 at 5:13
  • the value is 266
    – tr3online
    Feb 14, 2012 at 5:46

1 Answer 1

1

http://codex.wordpress.org/Function_Reference/image_downsize

Returns:
(bool|array) False on failure, array on success. Array with image url, width, height, and whether is intermediate size, in that order is returned on success is returned. $is_intermediate is true if $url is a resized image, false if it is the original.

I am going to assume the array within the array part is completely odd.

So let's start working backwards...

http://codex.wordpress.org/Function_Reference/wp_insert_attachment

Returns an array of key => value pairs containing path information on the currently configured uploads directory.

Example:

$upload_dir = wp_upload_dir();
echo $upload_dir['baseurl'];

I see that you have this instead:

$wpUploadPath = wp_upload_dir(); // this returns an array!
$target_path = $wpUploadPath; // now the target_path is an array?

That is setting the target path to an array instead of a string...

Also - target_path shouldn't be a full path..it should be the file name. The whole purpose of wp_upload_dir() is to determine the path.

The usage of wp_insert_attachement is as follows:

wp_insert_attachment( $attachment, $filename, $parent_post_id )

Notice how it says $filename.

Since you are passing $target_path as the second parameter into wp_insert_attachement() and that second parameter is intended to represent the filename, it makes the most sense that this is your problem.

1
  • Thanks. I think I was looking at the screen for too long!
    – tr3online
    Feb 14, 2012 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.