WordPress.org

Ready to get started?Download WordPress

Forums

Solution to large image thumbnail creation (and custom upload.php file) (1 post)

  1. Scytle
    Member
    Posted 7 years ago #

    After much trouble i have finally figured out why wordpress will not create thumbnails for images over a certain size (both file size in kb/mb and pixel size)

    Here goes.

    In both my wordpress 1.5.X and 2.X installation i have run into the problem that if you try to upload a picture over a certain size (file size or image size) then a thumbnail will not be created.

    At first i thought the problem was in wp_thumbnail_create() it was not. Then i found this little monster in admin-functions.php

    $thumbnail = imagecreatetruecolor($image_new_width, $image_new_height);

    i started to look through the GD library in php. I started to look into imagecreatetruecolor and found that it can take a lot of memory to handle large images

    http://qdig.sourceforge.net/Support/AllowedMemorySize

    so i shot over to my php.ini and changed this line

    memory_limit = 12M

    to

    memory_limit = 120M

    and wouldn't you know it SHAZAM now i can upload some freaking huge ass files and they spit out thumbnails and everything.

    Also for anyone still running wordpress 1.5.X here is my custom upload.php if anyone wants to use it. This little beast makes uploading images in wordpress 1.5.x much much easier. You will also need to add three classes to your css file (the css is to format the image in the post, you can change it to whatever you want).

    I am a php newb and not that good at css so if anyone finds anything that they could/would make better feel free to do so.

    you can see this in action here http://www.blog.thesietch.org

    .i_left {
    clear:both;
    float:left;
    margin:0 8px 8px 0;
    padding:4px;
    border:1px solid #eef;
    }

    .i_right {
    clear:both;
    float:right;
    margin:0 0 8px 8px;
    padding:4px;
    border:1px solid #eef;
    }
    .i_center {
    clear:both;
    margin-left: auto;
    margin-right: auto;
    padding:4px;
    border:1px solid #eef;
    }

    <?php
    require_once('admin.php');

    $title = 'Upload Image or File';

    require_once('admin-header.php');

    if ($user_level == 0) //Checks to see if user has logged in
    die (__("Cheatin' uh ?"));

    if (!get_settings('use_fileupload')) //Checks if file upload is enabled in the config
    die (__("The admin disabled this function"));

    if ( !get_settings('fileupload_minlevel') )
    die (__("You are not allowed to upload files"));

    $allowed_types = explode(' ', trim(strtolower(get_settings('fileupload_allowedtypes'))));

    if ($_POST['submit']) {
    $action = 'upload';
    } else {
    $action = '';
    }

    if (!is_writable(get_settings('fileupload_realpath')))
    $action = 'not-writable';
    ?>

    <div class="wrap">

    <?php
    switch ($action) {
    case 'not-writable':
    ?>
    <p><?php printf(__("It doesn't look like you can use the file upload feature at this time because the directory you have specified (%s) doesn't appear to be writable by WordPress. Check the permissions on the directory and for typos."), get_settings('fileupload_realpath')) ?></p>

    <?php
    break;
    case '':
    foreach ($allowed_types as $type) {
    $type_tags[] = "$type";
    }
    $i = implode(', ', $type_tags);
    ?>
    <p><?php printf(__('You can upload files with the extension %1$s as long as they are no larger than %2$s KB. If you’re an admin you can configure these values under options.'), $i, get_settings('fileupload_maxk'), 'options-misc.php') ?></p>
    <form action="upload.php" method="post" enctype="multipart/form-data">
    <p>
    <label for="img1"><?php _e('File:') ?></label>

    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo get_settings('fileupload_maxk') * 1024 ?>" />
    <input type="file" name="img1" id="img1" size="35" class="uploadform" /></p>
    <p>
    <label for="imgdesc"><?php _e('Description:') ?></label>
    <input type="text" name="imgdesc" id="imgdesc" size="30" class="uploadform" />
    </p>

    <!-- orient image stuff -->

    <p><?php _e('Orient Image?') ?></p>
    <p>
    <label for="orient_no">
    <input type="radio" name="orient" value="orientno" checked="checked" id="orient_no" />
    <?php _e('No thanks') ?></label>

    <label for="Orient_left">
    <input type="radio" name="orient" value="orientleft" id="orient_left" />
    <?php _e('left') ?></label>

    <label for="Orient_center">
    <input type="radio" name="orient" value="orientcenter" id="orient_center" />
    <?php _e('center') ?></label>

    <label for="Orient_right">
    <input type="radio" name="orient" value="orientright" id="orient_right" />
    <?php _e('right') ?></label>

    <!--link to thumbnail -->
    <p><?php _e('Link Thumbnail to Larger Image?') ?></p>
    <p>
    <label for="linkthumb_no">
    <input type="radio" name="linkthumb" value="linkthumbno" checked="checked" id="linkthumb_no" />
    <?php _e('No thanks') ?></label>

    <label for="linkthumb_yes">
    <input type="radio" name="linkthumb" value="linkthumbyes" id="linkthumb_yes" />
    <?php _e('yes (dont forget to check create thumb)') ?></label>

    <p><?php _e('Create a thumbnail?') ?></p>
    <p>
    <label for="thumbsize_no">
    <input type="radio" name="thumbsize" value="none" checked="checked" id="thumbsize_no" />
    <?php _e('No thanks') ?></label>

    <label for="thumbsize_small">
    <input type="radio" name="thumbsize" value="small" id="thumbsize_small" />
    <?php _e('Small (200px largest side)') ?></label>

    <label for="thumbsize_large">
    <input type="radio" name="thumbsize" value="large" id="thumbsize_large" />
    <?php _e('Large (400px largest side)') ?></label>

    <label for="thumbsize_custom">
    <input type="radio" name="thumbsize" value="custom" id="thumbsize_custom" />
    <?php _e('Custom size') ?></label>
    :
    <input type="text" name="imgthumbsizecustom" size="4" />
    <?php _e('px (largest side)') ?> </p>
    <p><input type="submit" name="submit" value="<?php _e('Upload File') ?>" /></p>
    </form>
    </div><?php
    break;
    case 'upload':

    $imgalt = basename( (isset($_POST['imgalt'])) ? $_POST['imgalt'] : '' );

    $img1_name = (strlen($imgalt)) ? $imgalt : basename( $_FILES['img1']['name'] );
    $img1_name = preg_replace('/[^a-z0-9_.]/i', '', $img1_name);
    $img1_size = $_POST['img1_size'] ? intval($_POST['img1_size']) : intval($_FILES['img1']['size']);

    $img1_type = (strlen($imgalt)) ? $_POST['img1_type'] : $_FILES['img1']['type'];
    $imgdesc = htmlentities2($_POST['imgdesc']);

    $pi = pathinfo($img1_name);
    $imgtype = strtolower($pi['extension']);

    if (in_array($imgtype, $allowed_types) == false)
    die(sprintf(__('File %1$s of type %2$s is not allowed.') , $img1_name, $imgtype));

    if (strlen($imgalt)) {
    $pathtofile = get_settings('fileupload_realpath')."/".$imgalt;
    $img1 = $_POST['img1'];
    } else {
    $pathtofile = get_settings('fileupload_realpath')."/".$img1_name;
    $img1 = $_FILES['img1']['tmp_name'];
    }

    // makes sure not to upload duplicates, rename duplicates
    $i = 1;
    $pathtofile2 = $pathtofile;
    $tmppathtofile = $pathtofile2;
    $img2_name = $img1_name;

    while ( file_exists($pathtofile2) ) {
    $pos = strpos( strtolower($tmppathtofile), '.' . trim($imgtype) );
    $pathtofile_start = substr($tmppathtofile, 0, $pos);
    $pathtofile2 = $pathtofile_start.'_'.zeroise($i++, 2).'.'.trim($imgtype);
    $img2_name = explode('/', $pathtofile2);
    $img2_name = $img2_name[count($img2_name)-1];
    }

    if (file_exists($pathtofile) && !strlen($imgalt)) {
    $i = explode(' ', get_settings('fileupload_allowedtypes'));
    $i = implode(', ',array_slice($i, 1, count($i)-2));
    $moved = move_uploaded_file($img1, $pathtofile2);
    // if move_uploaded_file() fails, try copy()
    if (!$moved) {
    $moved = copy($img1, $pathtofile2);
    }
    if (!$moved) {
    die(sprintf(__("Couldn't upload your file to %s."), $pathtofile2));
    } else {
    chmod($pathtofile2, 0666);
    @unlink($img1);
    }

    //

    // duplicate-renaming function contributed by Gary Lawrence Murphy
    ?>
    <p><?php __('Duplicate File?') ?></p>
    <p><b><?php printf(__("The filename '%s' already exists!"), $img1_name); ?></b></p>
    <p> <?php printf(__("Filename '%1\$s' moved to '%2\$s'"), $img1, "$pathtofile2 - $img2_name") ?></p>
    <p><?php _e('Confirm or rename:') ?></p>
    <form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo get_settings('fileupload_maxk') * 1024 ?>" />
    <input type="hidden" name="img1_type" value="<?php echo $img1_type;?>" />
    <input type="hidden" name="img1_name" value="<?php echo $img2_name;?>" />
    <input type="hidden" name="img1_size" value="<?php echo $img1_size;?>" />
    <input type="hidden" name="img1" value="<?php echo $pathtofile2;?>" />
    <input type="hidden" name="thumbsize" value="<?php echo $_REQUEST['thumbsize'];?>" />
    <input type="hidden" name="imgthumbsizecustom" value="<?php echo $_REQUEST['imgthumbsizecustom'];?>" />
    <?php _e('Alternate name:') ?>
    <input type="text" name="imgalt" size="30" class="uploadform" value="<?php echo $img2_name;?>" />

    <?php _e('Description:') ?>
    <input type="text" name="imgdesc" size="30" class="uploadform" value="<?php echo $imgdesc;?>" />

    <input type="submit" name="submit" value="<?php _e('Rename') ?>" class="search" />
    </form>
    </div>
    <?php

    require('admin-footer.php');
    die();

    }

    if (!strlen($imgalt)) {
    @$moved = move_uploaded_file($img1, $pathtofile); //Path to your images directory, chmod the dir to 777
    // move_uploaded_file() can fail if open_basedir in PHP.INI doesn't
    // include your tmp directory. Try copy instead?
    if(!$moved) {
    $moved = copy($img1, $pathtofile);
    }
    // Still couldn't get it. Give up.
    if (!$moved) {
    die(sprintf(__("Couldn't upload your file to %s."), $pathtofile));
    } else {
    chmod($pathtofile, 0666);
    @unlink($img1);
    }

    } else {
    rename($img1, $pathtofile)
    or die(sprintf(__("Couldn't upload your file to %s."), $pathtofile));
    }

    //orientation stuff
    if($_POST['orient'] != 'orientno' ) {
    if($_POST['orient'] == 'orientleft') {
    $orient = ' class = "i_left" ';
    }
    elseif($_POST['orient'] == 'orientright') {
    $orient = ' class = "i_right" ';
    }
    elseif($_POST['orient'] == 'orientcenter') {
    $orient = ' class = "i_center" ';
    }

    }

    if($_POST['orient'] == 'orientno'){
    $orient = ' ';
    }

    //link thumb stuff
    if($_POST['linkthumb'] == 'linkthumbyes' ) {
    $linkthumb = 'yes';
    }

    if($_POST['thumbsize'] != 'none' ) {
    if($_POST['thumbsize'] == 'small') {
    $max_side = 200;
    }
    elseif($_POST['thumbsize'] == 'large') {
    $max_side = 400;
    }
    elseif($_POST['thumbsize'] == 'custom') {
    $max_side = intval($_POST['imgthumbsizecustom']);
    }

    $result = wp_create_thumbnail($pathtofile, $max_side, NULL);
    if($result != 1) {
    print $result;
    }
    }

    if ( ereg('image/',$img1_type) )
    if($linkthumb == 'yes' ){
    $piece_of_code = " <img src='" . get_settings('fileupload_url') . "/thumb-$img1_name' $orient alt='$imgdesc' />";
    }
    else{
    $piece_of_code = "<img src='" . get_settings('fileupload_url') ."/$img1_name' $orient alt='$imgdesc' />";
    }
    else
    $piece_of_code = "$imgdesc";

    $piece_of_code = htmlspecialchars( $piece_of_code );
    ?>

    <h3><?php _e('File uploaded!') ?></h3>
    <p><?php printf(__("Your file %s was uploaded successfully!"), $img1_name); ?></p>
    <p><?php _e('Here’s the code to display it:') ?></p>
    <p><?php echo $piece_of_code; ?>
    </p>
    <p><?php _e('Image Details') ?>:
    Name:
    <?php echo $img1_name; ?>

    <?php _e('Size:') ?>
    <?php echo round($img1_size / 1024, 2); ?> <?php _e('KB') ?>
    <?php _e('Type:') ?>
    <?php echo $img1_type; ?>
    </p>
    </div>
    <p><?php _e('Upload another') ?></p>
    <?php
    break;
    }
    include('admin-footer.php');
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.