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

  • Home
  • SEARCH
  • 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 7548467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:42:21+00:00 2026-05-30T09:42:21+00:00

All, I’m uploading an image and then making it a thumbnail for display purposes.

  • 0

All,
I’m uploading an image and then making it a thumbnail for display purposes. I’m using the following code to process my upload:

$imageW = $blogOptions['image']['width'];
$imageH = $blogOptions['image']['height'];
$themePath = ABSPATH . 'wp-content/';
$path_to_image_directory = $themePath.'uploads/';  
$path_to_thumbs_directory = $themePath.'uploads/thumbs/';
$fieldname = 'logo';

if(preg_match('/[.](jpg)|(gif)|(png)|(JPG)$/', $_FILES[$fieldname]['name'])) {
$now = time();
$filename = $now."_".$_FILES[$fieldname]['name'];
$source = $_FILES[$fieldname]['tmp_name'];  
$target = $path_to_image_directory . $filename;  
move_uploaded_file($source, $target);

$file = $path_to_image_directory . $filename;
$x = @getimagesize($file);
switch($x[2]) { 
case 1: 
    $type = "gif";  
    break; 
case 2: 
    $type = "jpeg";
    break; 
case 3: 
    $type = "png";   
    break; 
default: 
    echo "error";
}

if($type == "gif"){
$im = imagecreatefromgif($path_to_image_directory . $filename);   
}if($type == "jpeg"){ 
$im = imagecreatefromjpeg($path_to_image_directory . $filename); 
}if($type == "png"){ 
$im = imagecreatefrompng($path_to_image_directory . $filename);   
} 

$ox = imagesx($im);  
$oy = imagesy($im);  

$nx = $imageW;  
$ny = $imageH;

$nm = imagecreatetruecolor($nx, $ny);  

imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);  

if(!file_exists($path_to_thumbs_directory)) {  
    if(!mkdir($path_to_thumbs_directory)) {  
        die("There was a problem. Please try again!");  
    }   
}

if($type == "gif"){
imagegif($nm, $path_to_thumbs_directory . $filename);   
}if($type == "jpeg"){ 
imagejpeg($nm, $path_to_thumbs_directory . $filename); 
}if($type == "png"){ 
imagepng($nm, $path_to_thumbs_directory . $filename);  
}
}
}

The upload works fine and the file is created successfully however when I display it is the image looks extremely distorted. If I use WordPress to handle the file upload and create the thumbnail it doesn’t look distorted at all. Is there a better method to upload this file or what am I don’t wrong to not lose the picture quality?

Thanks!

  • 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-05-30T09:42:23+00:00Added an answer on May 30, 2026 at 9:42 am

    You haven’t done any scaling – if you want to have a fixed thumbnail width and height it will be distorted.

    To do scaling, do the following:

    $original_width = imagesx($im);
    $original_height = imagesy($im);
    
    $scaling_factor = ($original_width / $desired_width);
    $new_width = $desired_width;
    $new_height = ($original_height / $scaling_factor);
    

    You’d then need to centre the new image and crop it to the desired height (if too tall). If too short, you should re-scale but use the height for the scaling factor, and then centre and crop if too wide.

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

Sidebar

Related Questions

All, Say I have the following bit of code: select: function(start, end, allDay) {
All, I have the following code: $fetch = mysql_query(SELECT * FROM calendar_events where event_status='booked'
All the examples I've come across using Google Maps API seem to show a
All of the examples I've seen of using yield return x; inside a C#
All, See the code below: function menu() { this.menuitem=[]; this.submenu=[]; this.menuitem[0] = $('div#sivname1'); this.menuitem[1]
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
All, I am building a small site using PHP. In the site, I receive
All, I'm using wordpress with this and for some reason the eventDrop stopped working.
All, I'm using Facebook Connect (JS SDK) to authenticate a user to my site.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.