Jump to content

Re-sizing picture and thumbnail


Panta

Recommended Posts

please i respect and appreciate this forum, am working on a project that needed people to upload picture which will go with a thumbnail, i have all that working but i really wish i could resize the picture to fixes Max. Height and a Max width, what i mean is that i want the pictures not to exceed some certin fixed height or weight. these are my codes all working fyn but i need help on how to fix the order. thanks as you input ur blessed efforts

<?phpdefine('UPLOADED_IMAGE_DESTINATION', 'uploads/');define('PROCESSED_IMAGE_DESTINATION', 'logo/');function process_image_upload($Field){    $temp_file_path = $_FILES[$Field]['tmp_name'];    $temp_file_name = $_FILES[$Field]['name'];    list(, , $temp_type) = getimagesize($temp_file_path);    if ($temp_type === NULL) {        return false;    }    switch ($temp_type) {        case IMAGETYPE_GIF:            break;        case IMAGETYPE_JPEG:            break;        case IMAGETYPE_PNG:            break;        default:            return false;    }    $uploaded_file_path = UPLOADED_IMAGE_DESTINATION . $temp_file_name;    $processed_file_path = PROCESSED_IMAGE_DESTINATION . preg_replace('/.[^.]+$/', '.jpg', $temp_file_name);    move_uploaded_file($temp_file_path, $uploaded_file_path);    /*     * PARAMETER DESCRIPTION     * (1) SOURCE FILE PATH     * (2) OUTPUT FILE PATH     * (3) THE TEXT TO RENDER     * (4) FONT NAME -- MUST BE A *FILE* NAME     * (5) FONT SIZE IN POINTS     * (6) FONT COLOR AS A HEX STRING     * (7) OPACITY -- 0 TO 100     * (8) TEXT ANGLE -- 0 TO 360     * (9) TEXT ALIGNMENT CODE -- POSSIBLE VALUES ARE 11, 12, 13, 21, 22, 23, 31, 32, 33     */    $result = create_watermark_from_string(        $uploaded_file_path,        $processed_file_path,        'www.hirectory.com.',        'Arial.ttf',        40,        'CCCCCC',        75,        45,        22    );    if ($result === false) {        return false;    } else {        return array($uploaded_file_path, $processed_file_path);    }}/* * Here is how to call the function(s) */$result = process_image_upload('File1');if ($result === false) {    echo '<br>An error occurred during file processing.';} else {    echo '<br>Original image saved as <a href="' . $result[0] . '" target="_blank">' . $result[0] . '</a>';    echo '<br>Watermarked image saved as <a href="' . $result[1] . '" target="_blank">' . $result[1] . '</a>';}?><form action="" method="post" enctype="multipart/form-data">Select a file to upload for processing<br><input type="file" name="File1"><br><input type="submit" value="Submit File"></form>
Edited by Panta
Link to comment
Share on other sites

I wrote this a while ago to resize images smaller. It will not resize larger. It will keep the same aspect ratio and make sure the image is smaller than the given width and height (if you give both, you don't need to).

function resize_image($opts){  $src = isset($opts['source']) ? $opts['source'] : '';  $dest = isset($opts['dest']) ? $opts['dest'] : '';  $w = isset($opts['w']) ? intval($opts['w']) : 0;  $h = isset($opts['h']) ? intval($opts['h']) : 0;  if ($src == '')  {    return;  }  if ($w == 0 && $h == 0)  {    return;  }  if ($dest == '')    $dest = $src; // resize in place  // open the image  $ext = strtolower(array_pop(explode('.', $src)));  switch ($ext)  {    case 'jpg':    case 'jpeg':      $i = imagecreatefromjpeg($src);    break;    case 'gif':      $i = imagecreatefromgif($src);    break;    case 'png':      $i = imagecreatefrompng($src);    break;    default:      return;  }  $new_w = imagesx($i);  $new_h = imagesy($i);  if (($w != 0 && $new_w <= $w && $h == 0) ||      ($w == 0 && $h != 0 && $new_h <= $h) ||      ($w != 0 && $new_w <= $w && $h != 0 && $new_h <= $h))  {    // image is small enough    if ($dest != $src)      copy($src, $dest);    return;  }  // determine new size  if ($w != 0 && $new_w > $w)  {    $new_h = ($w / $new_w) * $new_h;    $new_w = $w;  }  if ($h != 0 && $new_h > $h)  {    $new_w = ($h / $new_h) * $new_w;    $new_h = $h;  }  // resize  $new = imagecreatetruecolor($new_w, $new_h);  if ($ext == 'png' || $ext == 'gif')  {    $trnprt_indx = imagecolortransparent($i);    // If we have a specific transparent color    if ($trnprt_indx >= 0)    {      // Get the original image's transparent color's RGB values      $trnprt_color = imagecolorsforindex($i, $trnprt_indx);      // Allocate the same color in the new image resource      $trnprt_indx = imagecolorallocate($new, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);      // Completely fill the background of the new image with allocated color.      imagefill($new, 0, 0, $trnprt_indx);      // Set the background color for new image to transparent      imagecolortransparent($new, $trnprt_indx);    }    // Always make a transparent background color for PNGs that don't have one allocated already    elseif ($ext == 'png')    {      // Turn off transparency blending (temporarily)      imagealphablending($new, false);      // Create a new transparent color for image      $color = imagecolorallocatealpha($new, 0, 0, 0, 127);      // Completely fill the background of the new image with allocated color.      imagefill($new, 0, 0, $color);      // Restore transparency blending      imagesavealpha($new, true);    }  }  imagecopyresampled($new, $i, 0, 0, 0, 0, $new_w, $new_h, imagesx($i), imagesy($i));  imagedestroy($i);  // save the image  switch ($ext)  {    case 'jpg':    case 'jpeg':      imagejpeg($new, $dest);    break;    case 'gif':      imagegif($new, $dest);    break;    case 'png':      imagepng($new, $dest);    break;  }  imagedestroy($new);}
You pass it an array of options, e.g.:
resize_image([  'source' => 'input.jpg',   'dest' => 'output.jpg',   'w' => 200,  'h' => 400]);
You can also leave out the destination filename if you want to overwrite the original.
  • Like 1
Link to comment
Share on other sites

Thanks so much justsomeguy it works perfect I fixed the last line resize_image(( 'source' => 'input.jpg', 'dest' => 'output.jpg', 'w' => 200, 'h' => 400));Much love bro rep+

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...