Imagecreatefromstring (): data is not in recognized format in

This is how I get my image:

$coverurl  = 'https://api.someurl/api/v1/img/' . $somenumber . '/l';
//$iheaders contains: 'Content-type' => 'image/jpeg'
$iresponse = wp_remote_get($coverurl, $iheaders);
$img = $iresponse['body'];
$testimg = base64_encode($img);

When I repeat $ testimg with the img tag, everything works fine.

echo '<img class="attachment-shop_single size-shop_single wp-post-image" src="data:image/jpeg;base64,'.$testimg.'" width="274" />';

Since I need to convert the string to jpg and save it in my uploads folder, I tried to use imagecreatefromstring().

$imgx = imagecreatefromstring($testimg);
        if ($imgx !== false) {
            header('Content-Type: image/jpeg');
            imagejpeg($imgx);
            imagedestroy($imgx);
        } else {
            echo 'An error occurred.';
        }

But I can never save anything, due to the following warning:

 Warning: imagecreatefromstring(): Data is not in a recognized format in /etc.

When I echo $testimg, I get:

/9j/4AAQSkZJRgABAQAAAQABAAD ...Many number and characters.. Ggm2JUsEvfqnxAhGFDP/9k=

What should I do to get the createimagefromstringjob done? Do I need to change a string $testimg? Thank you for your interest.

+4
source share
2 answers

The method imagecreatefromstringdoes not take a base_64 encoded string. Try instead:

$imgx = imagecreatefromstring($img); // Contents of $iresponse['body']

(. ):

<?php
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
       . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
       . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
       . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);

$im = imagecreatefromstring($data);
+5

https://codex.wordpress.org/Function_Reference/wp_insert_attachment, :

//get the image from internet
$coverurl  = 'https://api.someurl/api/v1/img/' . $somenumber . '/l';
$iresponse = wp_remote_get($coverurl, $iheaders);
$img = $iresponse['body'];
$directory = "/".date('Y')."/".date('m')."/";
$wp_upload_dir = wp_upload_dir();
//encode $img as with html image tag
$imgdata = base64_encode($img);
$filename = "gtb_". $isbnraw.".jpg";
$fileurl = "../wp-content/uploads".$directory.$filename;
$filetype = wp_check_filetype( basename($fileurl), null);
file_put_contents($fileurl, $img);
$attachment = array(
    'guid' => $wp_upload_dir['url'] . '/' . basename( $fileurl ),
    'post_mime_type' => $filetype['type'],
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($fileurl)),
    'post_content' => '',
    'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $fileurl, $post_id);
require_once('../wp-admin/includes/image.php');
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata($attach_id, $fileurl);
wp_update_attachment_metadata($attach_id, $attach_data);
set_post_thumbnail($post_id, $attach_id);
//add media_category
wp_set_object_terms($attach_id, $mediacat, 'media_category');

base64 jpg Wordpress /. , . .

-1

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


All Articles