Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9186507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:32:43+00:00 2026-06-17T19:32:43+00:00

I am trying to make a thumbnail. And the php function works. I’ve made

  • 0

I am trying to make a thumbnail. And the php function works. I’ve made thumbnails with it. However, certain files do not work. I have narrowed down the problem, but am now stuck. Any help would be appreciated.

The images can be found at http://www.pspsigma.com/image_test.php .The one on the left will not work. The one on the right will. Both are .jpg. Both are originally uploaded via a form, but have been altered for stack overflow so that the function works without a form.

The html is a simple script with file input set action to this .php file

the php:

<?php
    $the_name= $_FILES['userfile']['name']; 
    $tmpname = $_FILES['userfile']['tmp_name'];  



    $size="1000";
      $save_dir="images/thumbs/";
      $save_name=$the_name;
      $maxisheight= "100";

function img_resize( $tmpname, $size, $save_dir, $save_name, $maxisheight)
{
$save_dir     .= ( substr($save_dir,-1) != "/") ? "/" : "";
$gis        = getimagesize($tmpname);
$type        = $gis[2];
switch($type)
    {
    case "1": $imorig = imagecreatefromgif($tmpname); break;
    case "2": $imorig = imagecreatefromjpeg($tmpname);break;
    case "3": $imorig = imagecreatefrompng($tmpname); break;
    default:  $imorig = imagecreatefromjpeg($tmpname);
    }

    //check if imorig is set
    if (!isset($imorig)) {
    echo " imorig is not set, ";    
    }

    //I tried setting $x and $y from the width and height of the original file. Still didn't work.
    //although the variables were set.
    //this makes me think the problem is with the imagecreatefromjpeg function.
    list($width, $height, $type, $attr) = getimagesize($tmpname);
    echo "getimagesize width= " . $width . ", ";
    echo "getimagesize height= " . $height . ", ";

    //possible solution, didn't work
    //$x = $width;
    //$y = $height;

    //This is the problem
    $x = imagesx($imorig);
    $y = imagesy($imorig);

    //check that x and y are set
    if (!isset($x) && !isset($y)) {
        echo" x and y are not set,";
    }
    elseif (!isset($x)) {
        echo" x is not set";
    }
    elseif (!isset($y)) {
        echo" y is not set";
    }
    else {
    echo "|x= " . $x . " |";
    echo "|y= " . $y . " |";            
    }


    $woh = (!$maxisheight)? $gis[0] : $gis[1] ;   


    if($woh <= $size)
    {
    $aw = $x;
    $ah = $y;
    }
        else
    {
        if(!$maxisheight){
            $aw = $size;
            $ah = $size * $y / $x;
        } else {
            $aw = $size * $x / $y;
            $ah = $size;
        }
    }   
    $im = imagecreatetruecolor($aw,$ah);
    if (!$im) {
        echo " failure: no im variable. ";
    }
    else {

if (imagecopyresampled($im,$imorig , 0,0,0,0,$aw,$ah,$x,$y)) {
    if (!imagejpeg($im, $save_dir.$save_name)) {
        echo "failure";}
        else {
        echo "success";}
}
else {
    echo" didn't copy resampled";
}
    }
}


img_resize( $tmpname, $size, $save_dir, $save_name, $maxisheight);
?>

If I choose the rush vader.jpg file and put it through this function. it echo’s that x and y are set, displays their values ,and success.

If I choose the calm.jpg it will echo “failure: no im variable.” and the $x and $y variables will not have values. If I assign the x and y variables the values from getimagesize($tmp_name) it echoes “didn’t copy resampled”.

Is this a problem with the actual image? or is there some way I can change the code so that every image works?

THANK YOU!!!

UPDATE

I echo’d every variable to see if something was off. The problem appears to be in the type. The type is output as 6 which from the php manual comments i found out is bmp.

I don’t understand how that could be if the extension is .jpg… If this is the case. Is a way to change the type to make it either .jpg, .png, or .gif?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:32:44+00:00Added an answer on June 17, 2026 at 7:32 pm

    The type from the image calm.jpg is 6 which is a bmp. Therefore the imagecreatefrom… will not work and $imorig is NULL. By adding a function from the php manual called imagecreatefrombmp with label 6. The function works perfecty.

    Add to the file:

    function imagecreatefrombmp($p_sFile)  { 
        //    Load the image into a string 
        $file    =    fopen($p_sFile,"rb"); 
        $read    =    fread($file,10); 
        while(!feof($file)&&($read<>"")) 
            $read    .=    fread($file,1024); 
    
        $temp    =    unpack("H*",$read); 
        $hex    =    $temp[1]; 
        $header    =    substr($hex,0,108); 
    
        //    Process the header 
        //    Structure: http://www.fastgraph.com/help/bmp_header_format.html 
        if (substr($header,0,4)=="424d") 
        { 
            //    Cut it in parts of 2 bytes 
            $header_parts    =    str_split($header,2); 
    
            //    Get the width        4 bytes 
            $width            =    hexdec($header_parts[19].$header_parts[18]); 
    
            //    Get the height        4 bytes 
            $height            =    hexdec($header_parts[23].$header_parts[22]); 
    
             //    Get the horz. resolution in pixel per meter, 4 bytes
            $dpix = hexdec($header_parts[39]. $header_parts[38]) * 0.0254;
    
            //    Get the vert. resolution in pixel per meter, 4 bytes
            $dpiy = hexdec($header_parts[43]. $header_parts[42]) * 0.0254;
    
            //    Unset the header params 
            unset($header_parts); 
        } 
    
        //    Define starting X and Y 
        $x                =    0; 
        $y                =    1; 
    
        //    Create newimage 
        $image            =    imagecreatetruecolor($width,$height); 
    
        //    Grab the body from the image 
        $body            =    substr($hex,108); 
    
        //    Calculate if padding at the end-line is needed 
        //    Divided by two to keep overview. 
        //    1 byte = 2 HEX-chars 
        $body_size        =    (strlen($body)/2); 
        $header_size    =    ($width*$height); 
    
        //    Use end-line padding? Only when needed 
        $usePadding        =    ($body_size>($header_size*3)+4); 
    
        //    Using a for-loop with index-calculation instaid of str_split to avoid large memory consumption
        //    Calculate the next DWORD-position in the body 
        for ($i=0;$i<$body_size;$i+=3) 
        { 
            //    Calculate line-ending and padding 
            if ($x>=$width) 
            { 
                //    If padding needed, ignore image-padding 
                //    Shift i to the ending of the current 32-bit-block 
                if ($usePadding) 
                    $i    +=    $width%4; 
    
                //    Reset horizontal position 
                $x    =    0; 
    
                //    Raise the height-position (bottom-up) 
                $y++; 
    
                //    Reached the image-height? Break the for-loop 
                if ($y>$height) 
                    break; 
            } 
    
            //    Calculation of the RGB-pixel (defined as BGR in image-data) 
            //    Define $i_pos as absolute position in the body 
            $i_pos    =    $i*2; 
            $r        =    hexdec($body[$i_pos+4].$body[$i_pos+5]); 
            $g        =    hexdec($body[$i_pos+2].$body[$i_pos+3]); 
            $b        =    hexdec($body[$i_pos].$body[$i_pos+1]); 
    
            //    Calculate and draw the pixel 
            $color    =    imagecolorallocate($image,$r,$g,$b); 
            imagesetpixel($image,$x,$height-$y,$color); 
    
            //    Raise the horizontal position 
            $x++; 
        } 
    
        //    Unset the body / free the memory 
        unset($body); 
    
        //    Return image-object 
        return ($image); 
        return ($dpix); 
        return ($dpiy); 
    } 
    

    add:

    case "6": $imorig = imagecreatefrombmp($tmpname); break;
    case "15": $imorig = imagecreatefromwbmp($tmpname); break;
    

    after

    case "3": $imorig = imagecreatefrompng($tmpname); break;
    

    Tested. and everything works. Thanks all!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a gallery with highslide. I have two thumbnails, a larger
I am trying to make a function in PHP that will allow me to
I am trying to modify the example http://www.html5rocks.com/en/tutorials/file/dndfiles/#toc-reading-files to make function handleFileSelect(evt) return reader.result;
I'm trying to make link_to function have a css class attached to it, My
I'm trying to make an thumbnail gallery (2x2) with twitter bootstrap, but I'm not
I'm trying to make some work after a response producer. In order to not
I am trying to make a title appear and disappear over a thumbnail. This
Trying to make a small countdown timer in my app but it's not working.
I have a list of 179 thumbnail images that I am trying to apply
I'm trying to make a thumbnail toolbar button (TTB from now on)visible when the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.