i/qicy.php
by webproger on 2023-09-21
ᐥqicy() image copyᐥ
<?php
function qicy($orgnl,$cpy,$wdth,$hght,$mode='prprtnl',$mthd='resample',$qlty=90) {
    // prprtnl,fxd_wdth,fxd_hght,fxd_all,prtn
    ini_set('memory_limit','100M');
    $sccss = false;
    $xtnsn_1 = Q_get_xtnsn($orgnl);
    $xtnsn_2 = Q_get_xtnsn($cpy);
    switch($xtnsn_1) {
        case 'jpg' : $img_orgn = imagecreatefromjpeg($orgnl); break;
        case 'jpeg': $img_orgn = imagecreatefromjpeg($orgnl); break;
        case 'gif' : $img_orgn = imagecreatefromgif ($orgnl); break;
        case 'png' : $img_orgn = imagecreatefrompng ($orgnl); break;
    }
    $wdth_orgn = imagesx($img_orgn);
    $hght_orgn = imagesy($img_orgn);
    $ratio_1 = $wdth_orgn/$hght_orgn;
    switch($mode) {
        case 'prprtnl':
            $ratio_2 = $wdth/$hght;
            if($ratio_1 > $ratio_2) {
                if($wdth_orgn > $wdth) $wdth_new = $wdth; else $wdth_new = $wdth_orgn;
                $hght_new = round($wdth_new/$ratio_1);
            } else {
                if($hght_orgn > $hght) $hght_new = $hght; else $hght_new = $hght_orgn;
                $wdth_new = round($hght_new*$ratio_1);
            }
            $strt_x   = 0;          $strt_y   = 0;
            $dstnc_x  = $wdth_orgn; $dstnc_y  = $hght_orgn;
            break;
        case 'fxd_wdth':
            $wdth_new = $wdth;      $hght_new = round($wdth_new/$ratio_1);
            $strt_x   = 0;          $strt_y   = 0;
            $dstnc_x  = $wdth_orgn; $dstnc_y  = $hght_orgn;
            break;
        case 'fxd_hght':
            $hght_new = $hght;      $wdth_new = round($hght_new*$ratio_1);
            $strt_x   = 0;          $strt_y   = 0;
            $dstnc_x  = $wdth_orgn; $dstnc_y  = $hght_orgn;
            break;
        case 'fxd_all':
            $ratio_2 = $wdth/$hght;
            $wdth_new = $wdth; $hght_new = $hght;
            if($ratio_1 > $ratio_2) {
                $dstnc_y = $hght_orgn;                     $dstnc_x = round($dstnc_y*$ratio_2);
                $strt_x  = round(($wdth_orgn-$dstnc_x)/2); $strt_y  = 0;
            } else {
                $dstnc_x = $wdth_orgn;                     $dstnc_y = round($dstnc_x/$ratio_2);
                $strt_x  = 0;                              $strt_y  = round(($hght_orgn-$dstnc_y)/2);
            }
            break;
        case 'prtn':
            $wdth_new = $wdth;                       $hght_new = $hght;
            $strt_x   = round(($wdth_orgn-$wdth)/2); $strt_y   = round(($hght_orgn-$hght)/2);
            $dstnc_x  = $strt_x+$wdth_new;           $dstnc_y  = $strt_y+$hght_new;
            break;
    }
    $img_new     = imagecreatetruecolor($wdth_new,$hght_new);
    $clr_bckgrnd = imagecolorallocate($img_new,255,255,255);
    imagefilledrectangle($img_new,0,0,$wdth_new,$hght_new,$clr_bckgrnd);
    
    if    ($mthd=='resize'  ) imagecopyresized(  $img_new,$img_orgn,0,0,$strt_x,$strt_y,$wdth_new,$hght_new,$dstnc_x,$dstnc_y);
    elseif($mthd=='resample') imagecopyresampled($img_new,$img_orgn,0,0,$strt_x,$strt_y,$wdth_new,$hght_new,$dstnc_x,$dstnc_y);
    switch($xtnsn_2) {
        case 'jpg': $sccss = imagejpeg($img_new,$cpy,$qlty); break;
        case 'gif': $sccss = imagegif ($img_new,$cpy,$qlty); break;
        case 'png': $sccss = imagepng ($img_new,$cpy,$qlty); break;
    }
    imagedestroy($img_orgn);
    imagedestroy($img_new);
    return $sccss;
}
function Q_duplicate_img($orgnl,$cpy,$wdth,$hght,$mode='prprtnl',$mthd='resample',$qlty=90) {
    qicy($orgnl,$cpy,$wdth,$hght,$mode='prprtnl',$mthd='resample',$qlty=90);
}
?>