• I’m trying to create my first plugin. The bit that I’m working on now is supposed to read the post title and create an image with that text in it. But for some reason it’s not saving the image. Here’s what I have:

    // Get the post title from the post
    $post_data = get_post($post->ID, ARRAY_A);
    $post_slug = $post_data['post_name'];
    $post_title = get_the_title( $post_id );
    
    // Generate the image
    $new_img = imagecreatetruecolor( 200, 80 );
    $background = imagecolorallocate( $new_img, 200, 200, 200 );
    $text_color = imagecolorallocate( $new_img, 150, 100, 100 );
    $line_color = imagecolorallocate( $new_img, 128, 255, 0 );
    imagestring( $new_img, 4, 30, 25, "$post_title", $text_color );
    imagesetthickness ( $new_featured_img, 5 );
    imageline( $new_featured_img, 30, 45, 165, 45, $line_color );
    
    // Save the image
    $upload_dir = wp_upload_dir();
    $upload_dir_path = $upload_dir['baseurl'];
    imagejpeg( $new_featured_img, "$upload_dir_path/$post_slug.jpg" );
    imagecolordeallocate( $new_featured_img, $line_color );
    imagecolordeallocate( $new_featured_img, $text_color );
    imagecolordeallocate( $new_featured_img, $background );
    }

    Here are the warnings it gives me:

    Warning: imagejpeg() [function.imagejpeg]: Unable to open ‘http://pastorhuff.com/wp-content/uploads/what-happens-in-a-fallen-world.jpg’ for writing: No such file or directory in /home/pastor/public_html/wp-content/plugins/auto-featured-image-from-title/auto-featured-image-from-title.php on line 35

    Warning: Cannot modify header information – headers already sent by (output started at /home/pastor/public_html/wp-content/plugins/auto-featured-image-from-title/auto-featured-image-from-title.php:35) in /home/pastor/public_html/wp-admin/post.php on line 222

    Warning: Cannot modify header information – headers already sent by (output started at /home/pastor/public_html/wp-content/plugins/auto-featured-image-from-title/auto-featured-image-from-title.php:35) in /home/pastor/public_html/wp-includes/pluggable.php on line 875

    Would greatly appreciate any guidance. Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I believe the file to receive the image with imagejpeg() must already exist, even if empty.

    As for the header already sent errors, your script is apparently attempting to redirect to another page after content has already been sent to the browser. PHP redirects only work if no content has been sent. After content is sent, you need to use javascript to have the client generate a new request.

    whether in trying to link insert image is correct? 😀

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Working on my first plugin’ is closed to new replies.