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
https://forums.phpfreaks.com/topic/40733-cropresize-image-functions-errors/
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...

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.