Jump to content

need help with image resize script


acctman

Recommended Posts

this is what I have so far... I need to check the image size and if the width is greater than 640w or 600h. I think i have the part correct where it keeps it within an aspect ratio I just need help with getting the width and height and making sure everything else is correct.

 

$src = "/home/site/public_html/images/backup/" . $imagename; 
$imgsize = getimagesize($src);
  	
  // Resize if image is bigger than 640w / 600h		
	if ($width > 640 || $height >= 600 ) {

// Rebuild the image
$target_width = 640;
$target_height = 600;
$target_ratio = $target_width / $target_height;
$img_ratio = $width / $height;
}

if ($target_ratio > $img_ratio) {
	$new_height = $target_height;
	$new_width = $img_ratio * $target_height;
} else {
	$new_height = $target_width / $img_ratio;
	$new_width = $target_width;
}

if ($new_height > $target_height) {
	$new_height = $target_height;
}
if ($new_width > $target_width) {
	$new_height = $target_width;
}

// Image will always be a jpg
$new_img = ImageCreateTrueColor($new_width, $new_height);
if (!@imagecopyresampled($new_img, $img, ($target_width-$new_width)/2, ($target_height-$new_height)/2, 0, 0, $new_width, $new_height, $width, $height)) {
	echo "ERROR:Could not resize image";
	exit(0);
}

// Use a output buffering to load the image into a variable
imagejpeg($new_img);
imagedestroy($new_img);

Link to comment
Share on other sites

how does this function an if statement blow look?

 


function resize($src,$dest) {
$source_pic = ($src);
$destination_pic = ($dest);
$max_width = 500;
$max_height = 600;

$msrc = imagecreatefromjpeg($source_pic);
list($width,$height) = getimagesize($source_pic);

$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;

if(($width <= $max_width) && ($height <= $max_height)){
    $tn_width = $width;
    $tn_height = $height;
    } elseif (($x_ratio * $height) < $max_height){
        $tn_height = ceil($x_ratio * $height);
        $tn_width = $max_width;
    } else {
        $tn_width = ceil($y_ratio * $width);
        $tn_height = $max_height;
}

$tmp = imagecreatetruecolor($tn_width,$tn_height);
imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);
imagejpeg($tmp,$destination_pic,100);
imagedestroy($msrc);
imagedestroy($tmp);
}

$width = imageSX($src);
$height = imageSY($src);

if ($width > 500) {
resize($src,$dest);
echo "Copied Image: $imagename\n";
} else {

if(copy($src, $dest)) {
    echo "Copied Image: $imagename\n";
} else {
    echo "Image Copy Failed: $imagename\n";
}
}

Link to comment
Share on other sites

I know this is a lot to ask but if can someone read through my code and make sure everything looks okay. everything about Watermark image works fine its been test, but i've added a lot of watermarking, resizing and thumbnailing coding at the bottom and i need to make sure that processes everything correctly.

 

<?php
mysql_connect("localhost", "**", "***");
mysql_select_db("db_main");

$sourcepath = "/home/ratemy/public_html/images/pictures/";

$query = "SELECT `m_id`, `m_date` FROM `rate_members`";
$result = mysql_query($query);

while($member = mysql_fetch_array($result)) {
  $year = date("Y", $member['m_date']);
  $month = date("m", $member['m_date']);
  $query2 = "SELECT `i_id`,`i_private`,`i_status` FROM `rate_pictures` WHERE `i_user` = '".$member['m_id']."'";
  $result2 = mysql_query($query2);
  if(!is_dir("/home/rmbimgs/public_html/" . $year)) {
    mkdir("/home/rmbimgs/public_html/" . $year);
  }
  if(!is_dir("/home/rmbimgs/public_html/" . $year . "/" . $month)) {
    mkdir("/home/rmbimgs/public_html/" . $year . "/" . $month);
  }  
  if(!is_dir("/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'])) {
    if(mkdir("/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'])) {
      echo "Made Directory:/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] . "\n";
    } else {
      echo "Failed Making Directory: /home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] . "\n";
    }
    if(mkdir("/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgPub")) {
       echo "Made Directory:" . "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgPub\n";
    } else {
      echo "Failed Making Directory: " . "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgPub\n";
    }
    if(mkdir("/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgExp")) {
       echo "Made Directory:" . "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgExp\n";
    } else {
      echo "Failed Making Directory: " . "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgExp\n";
    }
    if(mkdir("/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgPvt")) {
       echo "Made Directory:" . "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgPvt\n";
    } else {
      echo "Failed Making Directory: " . "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgPvt\n";
    }
    if(mkdir("/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgMob")) {
       echo "Made Directory:" . "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgMob\n";
    } else {
      echo "Failed Making Directory: " . "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgMob\n";
    }
    if(mkdir("/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgBak")) {
       echo "Made Directory:" . "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgBak\n";
    } else {
      echo "Failed Making Directory: " . "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgBak\n";
    }	
    if(mkdir("/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgPub/imgTmb")) {
       echo "Made Directory:" . "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgPub/imgTmb\n";
    } else {
      echo "Failed Making Directory: " . "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgPub/imgTmb\n";
    }
    if(mkdir("/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgExp/imgTmb")) {
       echo "Made Directory:" . "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgExp/imgTmb\n";
    } else {
      echo "Failed Making Directory: " . "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgExp/imgTmb\n";
    }
    if(mkdir("/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgPvt/imgTmb")) {
       echo "Made Directory:" . "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgPvt/imgTmb\n";
    } else {
      echo "Failed Making Directory: " . "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgPvt/imgTmb\n";
    }
    if(mkdir("/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgMob/imgTmb")) {
       echo "Made Directory:" . "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgMob/imgTmb\n";
    } else {
      echo "Failed Making Directory: " . "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgMob/imgTmb\n";
    } 
}
  while($picture = mysql_fetch_array($result2)) {
    $imagename = $member['m_id'] . "-" . $picture['i_id'] . ".jpg";
    if($picture['i_private'] == 0) {  	
$src = "/home/ratemy/public_html/images/backup/" . $imagename; 

    	$oldsrc = "/home/ratemy/public_html/images/pictures/" . $imagename;
//      $thumb_src = "/home/ratemy/public_html/images/thumbnails/" . $imagename;

      $dest = "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgPub/" . $imagename;
      $thumb_dest = "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgPub/imgTmb/" . $imagename;
    } else if ($picture['i_private'] == 2) {
$src = "/home/ratemy/public_html/images/backup/" . $imagename; 

   		$oldsrc = "/home/ratemy/public_html/images/pictures/" . $imagename;
//      $thumb_src = "/home/ratemy/public_html/images/thumbnails/" . $imagename;

      $dest = "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgExp/" . $imagename;
      $thumb_dest = "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgExp/imgTmb/" . $imagename;
    } else if ($picture['i_private'] == 1) {
$src = "/home/ratemy/public_html/images/backup/" . $imagename; 

    	$oldsrc = "/home/ratemy/private/pictures/" . $imagename;
//      $thumb_src = "/home/ratemy/private/thumbnails/" . $imagename;

      $dest = "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgPvt/" . $imagename;
      $thumb_dest = "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgPvt/imgTmb/" . $imagename;
    }
    $back_src = "/home/ratemy/public_html/images/backup/" . $imagename; 
    $back_dest = "/home/rmbimgs/public_html/" . $year . "/" . $month . "/" . $member['m_id'] ."/imgBak/" . $imagename;


/*
1. grab backup image. get width and check to see if its over 500px
if yes then resize keeping aspect ratio with Max width of 500

2. copy backup image to new backup location.

3. watermark image.

4. copy watermarked image to its new location

5. create thumbnail 200x200 perfect sqrthumb for Main image if i_status = 2

6. copy new main thumb to new location

7. create regular thumbnail 70x70

8. copy new regular thumbnail to new location
*/

//// Watermark Image ///////////////////////////////////////
  function water_mark($watermark, $src) {
  	header("Content-Type: image/jpeg");
  $watermark = imagecreatefrompng($watermark);
  imagealphablending($watermark, false);
imagesavealpha($watermark, true);
  list($watermark_width,$watermark_height) = getimagesize('wmark-rmb.png');
  $image = imagecreatefromjpeg($src);       
  $size = getimagesize($src);
  $dest_x = ($size[0] / 2) - ($watermark_width / 2); 
$dest_y = ($size[1] / 2) - ($watermark_height / 2);
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);

imagejpeg($image); 
imagedestroy($image);
imagedestroy($watermark); 
    }
//water_mark::apply('wmark-rmb.png','test.jpg');  
///////////////////////////////////////////////////////////
///// Resize Image ////////////////////////////////////////
function resize($src,$dest) {
$source_pic = ($src);
$destination_pic = ($dest);
$max_width = 500;
$max_height = 600;

$msrc = imagecreatefromjpeg($source_pic);
list($width,$height) = getimagesize($source_pic);

$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;

if(($width <= $max_width) && ($height <= $max_height)){
    $tn_width = $width;
    $tn_height = $height;
    } elseif (($x_ratio * $height) < $max_height){
        $tn_height = ceil($x_ratio * $height);
        $tn_width = $max_width;
    } else {
        $tn_width = ceil($y_ratio * $width);
        $tn_height = $max_height;
}
$tmp = imagecreatetruecolor($tn_width,$tn_height);
imagecopyresampled($tmp,$msrc,0,0,0,0,$tn_width, $tn_height,$width,$height);
imagejpeg($tmp,$destination_pic,90);

//add watermak//
water_mark('wmark-rmb.png',$destination_pic);

imagedestroy($msrc);
imagedestroy($tmp);
}
////////////////////////////////////////////////////////////
/// Create Thumbnail ///////////////////////////////////////
function CreateSquareThumb($source,$tdest,$new_width,$new_height,$border=0) {
//    $new_width = 60;
//    $new_height = 60;
    $sourcedate = 0;
    $destdate = 0;
    global $convert;
    if (file_exists($tdest)) {
       clearstatcache();
       $sourceinfo = stat($source);
       $destinfo = stat($tdest);
       $sourcedate = $sourceinfo[10];
       $destdate = $destinfo[10];
    }
    if (!file_exists($tdest) or ($sourcedate > $destdate)) {
       global $ImageTool;
       $imgsize = GetImageSize($source);
       $width = $imgsize[0];
       $height = $imgsize[1];

    if ($width > $height) {
       $xoord = ceil(($width - $height) / 2 );
       $width = $height;
    } else {
       $yoord = ceil(($height - $width) / 2);
       $height = $width;
    }
       $new_im = imagecreatetruecolor($new_width,$new_height);
       $im = imagecreatefromjpeg($source);
      imagecopyresampled($new_im,$im,0,0,$xoord,$yoord,$new_width,$new_height,$width,$height);
      imagejpeg($new_im,$tdest,90);
      imagedestroy($new_im);
		imagedestroy($im);
    }
}
////////////////////////////////////////////////////////////



$width = imageSX($src);
$height = imageSY($src);

if(file_exists($src)) {
if ($width > 500) {
	resize($src,$dest);
	echo "Copied Image: $imagename\n";
} else {
	if(copy($src, $dest)) {
	  echo "Copied Image: $imagename\n";
	} else {
  echo "Image Copy Failed: $imagename\n";
	}
}
} else if(!file_exists($src)) {
if ($width > 500) {
	resize($oldsrc,$dest);
	echo "Old Copied Image: $imagename\n";
} else {
	if(copy($oldsrc, $dest)) {
	  echo "Old Copied Image: $imagename\n";
	} else {
  echo "Old Image Copy Failed: $imagename\n";
	}
}
}

if($picture['i_status'] == 2) { // Check, see if this is the Main Profile Photo
CreateSquareThumb($dest,$thumb_dest,200,200);
	echo "Main Copied Thumbnail: $imagename\n";
} else {
      echo "Main Thumbnail Copy Failed: $imagename\n";
    }

if($picture['i_status'] != 2) {
    if(copy($thumb_src, $thumb_dest)) {
    CreateSquareThumb($dest,$thumb_dest,70,70);
      echo "Copied Thumbnail: $imagename\n";
    } else {
      echo "Thumbnail Copy Failed: $imagename\n";
    }
  }

if(file_exists($back_src)) {
    if(copy($back_src, $back_dest)) {
      echo "Copied Backup: $imagename\n";
    } else {
      echo "Backup Copy Failed: $imagename\n";
    }
} else if(!file_exists($back_src)) {
    if(copy($oldsrc, $back_dest)) {
      echo "Old Copied Backup: $imagename\n";
    } else {
      echo "Old Backup Copy Failed: $imagename\n";
    }
}   
  }

}
?>

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.