Here is a complete function I wrote a few months ago. It should do it for you or give you hints for fixing your problem.

René fournier wrote:

Hello,

I have a function that is meant to check if an image is greater than
a certain width and height, and if it is, downsample it. The checking
part works fine. Downsampling is not happening though. Here's what I've got
(btw, $file = "/somedirectory/photo.jpg"):


$src_img=imagecreatefromJPEG($file); $dst_img=imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_width,$new_height,$siz e[0],$size[1]);
imagejpeg($dst_img,$file,$img_quality);


Any ideas?

Thanks.

...Rene

<?php
/**
 * Resize an image to a new width.  Original image portion is maintained.
 * 
 * Has two modes: 
*  
* $auto == "yes,  looks at $_FILES['username']['error'] to determine if a file was just uploaded. 
* If so, it gets the file name from $_FILES	and uses $file_location for the path, relative
* to the route.
* 
*  Or else, it uses the file $file_location, which must be the full path, relative to the route.  
*   
 * image resizer arguments:
 * $resize_args= array("quality" => 100, "new_width" => 200, "quality" =>100, "backup_org" => "no");
 * 
 *       quality = percent [e.g. "quality" => 90] meaning 90%
 *       width = pixels [e.g., "new_width" => 200] i.e., 200 pixels.
 *       backup_org [original file] "yes" or "no".  "no" is the default
 * 
 * Use this to fetch $resize_stats:
 * 		foreach($resize_stats as $key => $value)
 * {
 * echo $key . " = " . $value;
 * }
 */


function resize_width($file_location, $resize_args, $auto)
{
    global $DOCUMENT_ROOT;

    if (empty($resize_args)) die("<p style=\"color:red\">Code error, argument(s) in \$resize_arg missing in resize_width. </p> ");

	if($auto== "yes"){

		if ($_FILES['username']['error'] == 0) {
	        
			$name = key($_FILES); 							//could use the register variables, but this is safer.
			
	        $org_img = $_FILES[$name]['name'];
	
	        $org_img = filename_fixer($org_img);
			
			$org_img = $file_location . $org_img;
		}
	}
	else{$org_img = $file_location;}
	
    if ($resize_args['backup_org'] == "yes") {
        file_backup($org_img);
    } 

    $org_img = $DOCUMENT_ROOT . $org_img;
	
	if (!file_exists($org_img))die("<p style=\"color:red\">Code error, $org_img missing or incorrect file name in resize_width()</p> </body></html>");

    $new_width = $resize_args['new_width'];
    $quality = $resize_args['quality'];

    $imagehw = GetImageSize($org_img);
    $org_width = $imagehw[0];
    $org_height = $imagehw[1];

    if ($new_width !== $org_width) {
        $imagevsize = $org_height * $new_width / $org_width;
        $new_height = ceil($imagevsize);

        $src_img = imagecreatefromjpeg($org_img);
        $dest_img = imagecreatetruecolor($new_width, $new_height);
        imagecopyresampled($dest_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, $org_width, $org_height);
        imagejpeg($dest_img, $org_img, $quality);
        imagedestroy($src_img);
        imagedestroy($dest_img);
    } else {
        $newheight = $org_height;
    } 
    // see above how to use array
    $resize_stats = array("Orginal width" => $org_width, "Orginal height" => $org_height, "New width" => $new_width, "New height" => $new_height);

    if ($resize_args['show_stats'] == "yes") {
        foreach($resize_stats as $key => $value) {
            echo $key . " = " . $value . "<br>\r\n";
        } 
    } 

    return $resize_stats;
} 


?>
-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to