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 8498815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:30:20+00:00 2026-06-11T00:30:20+00:00

I am using the following code in html to call a php file to

  • 0

I am using the following code in html to call a php file to create a thumbnail and show it on this page
&w=150&h=&00″ alt=”Image” />

the code of miniature.php is the following :

     <?php

     function redimensionner_image($chemin_image, $largeur_max, $hauteur_max)
    {
list($src_w, $src_h) = getimagesize($chemin_image);
$dst_w = $largeur_max;
$dst_h = $hauteur_max;

if($src_w < $dst_w)
    $dst_w = $src_w;

// Teste les dimensions tenant dans la zone
$test_h = round(($dst_w / $src_w) * $src_h);
$test_w = round(($dst_h / $src_h) * $src_w);

if(!$dst_h)// Si Height final non précisé (0)
    $dst_h = $test_h;
elseif(!$dst_w) // Sinon si Width final non précisé (0)
    $dst_w = $test_w;
elseif($test_h>$dst_h) // Sinon teste quel redimensionnement tient dans la zone
    $dst_w = $test_w;
else
    $dst_h = $test_h;

$array_ext = explode('.', $chemin_image);
$extension = strtolower($array_ext[count($array_ext)-1]);

if($extension == 'jpg' || $extension == 'jpeg')
   $img_in = imagecreatefromjpeg($chemin_image);
else if($extension == 'png')
   $img_in = imagecreatefrompng($chemin_image);
else if($extension == 'gif')
   $img_in = imagecreatefromgif($chemin_image);
else
    return false;

$img_out = imagecreatetruecolor($dst_w, $dst_h);
imagecopyresampled($img_out, $img_in, 0, 0, 0, 0, $dst_w, $dst_h, imagesx($img_in), imagesy($img_in));

imagejpeg($img_out);
     }

      ?>

However, Imagecreatefromjpeg returns a black image after resize. Any help please

  • 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-11T00:30:22+00:00Added an answer on June 11, 2026 at 12:30 am

    First you don’t need to explode the file-extension as getimagesize will give you the type:

    list($width, $height, $type) = getimagesize($source);
    

    Next I don’t follow your size calculations, try to simplify them, f.e.:

    $scale = min($maxWidth / $width, $maxHeight / $height, 1); // We only use downsampling, no upsampling! If you need upsampling remove the '1' parameter
    
    $newWidth = min($width * $scale, $maxWidth);
    $newHeight = min($height * $scale, $maxHeight);
    

    Simple rescaling and preserves aspect ratio.

    For reference, here is the code I use in my projects:

    function SaveImageAsJpeg($sourceFilename, $destFilename, $maxWidth = 0, $maxHeight = 0, $jpegQuality = 80) {
        list($width, $height, $type) = getimagesize($sourceFilename);
    
        $sourceImage = false;
    
        switch ($type) {
            case IMAGETYPE_GIF:
                $sourceImage = imagecreatefromgif($sourceFilename); 
                break;
            case IMAGETYPE_JPEG:
                $sourceImage = imagecreatefromjpeg($sourceFilename);
                break;
            case IMAGETYPE_PNG:
                $sourceImage = imagecreatefrompng($sourceFilename);
                break;
        }
    
        if (!$sourceImage)
            return false;
    
        if (($maxWidth == 0) || ($maxHeight == 0)) {
            // Don't resize
            $destinationImage = imagecreatetruecolor($width, $height);
            imagecopy($destinationImage, $sourceImage, 0, 0, 0, 0, $width, $height);
            imagejpeg($destinationImage, $destFilename, $jpegQuality);
            imagedestroy($destinationImage);
        } else {
            // Resize image
            $scale = min($maxWidth / $width, $maxHeight / $height, 1);  // We only use downsampling, no upsampling! If you need upsampling remove the '1' parameter
    
            $newWidth = min($width * $scale, $maxWidth);
            $newHeight = min($height * $scale, $maxHeight);
    
            $destinationImage = imagecreatetruecolor($newWidth, $newHeight);
            imagecopyresampled($destinationImage, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
            imagejpeg($destinationImage, $destFilename, $jpegQuality);
            imagedestroy($destinationImage);
        }
    
        imagedestroy($sourceImage);
    
        return true;
    }
    

    Of course, above code will not return image data. It just saves the rescaled image to an other file on the server. But if you pass NULL as $destFilename parameter, it will output the image data to the output stream:

    header('Content-Type: image/jpeg');
    SaveImageAsJpeg($sourceFilename, NULL, 200, 200);
    

    If you’re still getting a black image, I would suggest increasing the PHP memory limit. If you can modify PHP.INI, adjust the memory_limit setting. Otherwise use a .htaccess file with this line, f.e.: php_value memory_limit 64M.

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

Sidebar

Related Questions

How to call the following two lines in magento's \app\code\core\Mage\Wishlist\Helper\Data.php file $this->_helper->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(); Actually
I'm using the following JSoup code to render this currency: currencySpan.html(&euro; + currencyFormat.format(estimatedValue)); However
I tried using the following code for a HTML page, but it doesn't work.
I'm currently downloading an HTML page, using the following code: Try Dim req As
I'm using PHP's HTTPRequest to call a webservice with the following code: <?php $req
I am querying a php script using js with following func call: $.post(http://localhost/abc/create.php,{ name:data
I'm using the following code to call in some html, and display it. Most
I open a pop-up window using following code in main.html function openwindow(url) { window.open(url,
I am using the following code to fetch html source website. private string Extract_Source(string
In my view I have the following code: @using (Html.BeginForm()) { @Html.TextBox(Date2, Model.Date2) <br/>

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.