Jump to content

Crop/Resize Image Functions errors


13th_Star

Recommended Posts

I have recently made two functions one to resize an image and one to crop it, only some times when I try to upload photos at a certain size lets say 800x600px, it gives me some errors and just shows the pictures as plain black, I can't really work out whats up with it so if someone does not mind could they take a look at my functions.

 

thanks.

 

the errors:

 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/bpotts/public_html/image_functions.php on line 90

 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/bpotts/public_html/image_functions.php on line 48

 

the functions: ( i have marked what line 48 and 90 are)

 


<?php


//Image functions// Crop, Reisize.


function ResizeImage($nw, $nh, $source, $stype, $dest) {

    $size = getimagesize($source);
    $w    = $size[0];
    $h    = $size[1];

    switch($stype) {
        case 'gif':
        $simg = imagecreatefromgif($source);
        break;
        case 'jpg':
        $simg = imagecreatefromjpeg($source);
        break;
        case 'jpeg':
        $simg = imagecreatefromjpeg($source);
        break;
        case 'png':
        $simg = imagecreatefrompng($source);
        break;
    }

   if ($w > $nw) {
    $n_h = $h * ($nw/$w);
    $n_w = ($n_h / $h) * $w;
   }
   else if ($w < $nw && $h > $nh) {
   $n_h = $w * ($nh/$h);
   $n_w = ($n_w / $w) * $h;
   }
   else {
   $n_h = $h;
   $n_w = $w;
   }

    $dimg = imagecreatetruecolor($n_w, $n_h);
    imagecopyresampled($dimg,$simg,-0,0,0,0,$n_w,$n_h,$w,$h);

    imagejpeg($dimg,$dest, "100");   

}
//this is line 48!

function CropImage($nw, $nh, $source, $stype, $dest) {

    $size = getimagesize($source);
    $w = $size[0];
    $h = $size[1];

    switch($stype) {
        case 'gif':
        $simg = imagecreatefromgif($source);
        break;
        case 'jpg':
        $simg = imagecreatefromjpeg($source);
        break;
        case 'jpeg':
        $simg = imagecreatefromjpeg($source);
        break;
        case 'png':
        $simg = imagecreatefrompng($source);
        break;
    }

    $dimg = imagecreatetruecolor($nw, $nh);

    $wm = $w/$nw;
    $hm = $h/$nh;

    $h_height = $nh/2;
    $w_height = $nw/2;

    if($w > $h) {

        $adjusted_width = $w / $hm;
        $half_width     = $adjusted_width / 2;
        $int_width      = $half_width - $w_height;

        imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);

    } elseif(($w < $h) || ($w == $h)) {

        $adjusted_height = $h / $wm;
        $half_height = $adjusted_height / 2;
        $int_height = $half_height - $h_height;
//THIS IS LINE 91
        imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);

    } else {
        imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
    }

    imagejpeg($dimg,$dest, "100");
} 

?>


Link to comment
Share on other sites

hello...

use..:

<?php

// img need reassimple 
$filename = '1.jpg';

// get new imag time stamp name
$tt= time();
$t="$tt.jpg";
// Set a maximum height and width
$width = 400;
$height = 500;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, $t, 100);

?> 

 

check it please...

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.