Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9017717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:20:42+00:00 2026-06-16T04:20:42+00:00

I have an application that has to crop images with variable width & height.

  • 0

I have an application that has to crop images with variable width & height. but i don’t know how to do this with the php gd (Createimagefromjpeg) function

in my code i have:

$targ_w = 400;
$targ_h = 400;

This means that the cropped image will always get this width and height. that’s not what i want. i want, in some way crop the images and crop it like i selected it at the crop area like in this picture:

cropped image

now when i crop that image, like in the picture i get this:

square image created

It is a square image because i have to give a width and height. but at every image i crop the sizes are different.

Is there a way (variables, id etc..) to do this?

Thanks 😀

EDIT: my code to create the cropped image:

<!DOCTYPE>
<html>
<head>
    <title>Cropped Image</title>
</head>
<body>

<?php
SESSION_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$targ_w = 400;
$targ_h = 400;
$jpeg_quality = 100;

$src = $_SESSION['target_path'];
$img_r = imagecreatefromjpeg($src);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['h']);

header('Content-type: image/jpeg');
imagejpeg($dst_r, 'data/uploads/' . basename($src));
header('Location:'.$src);
exit;
}

?>

</body>
</html>

My code to upload the image:

<!DOCTYPE>
<html>

<head>
    <title>Het Vergeet-mij-nietje</title>
    <link href="style/default.css" REL="stylesheet" TYPE="text/css">
    <script type="text/javascript" src="js/showfunctie.js"></script>
    <script src="js/jquery.min.js"></script>
    <script src="js/jquery.Jcrop.min.js"></script>
    <link rel="stylesheet" href="css/jquery.Jcrop.css" type="text/css" />
</head>

<body>
<center>
    <div id="title">
    <h1><a href="index.php" id="link1">Het "Vergeet-mij-nietje"</a></h1>
    <h3>Upload Systeem</h3>
    </div>

<div id="content1">
    <p><b>Upload hier een afbeelding en druk op upload om hem vervolgens te kunnen bijsnijden.</b></p>
    <form action="uploaded.php" method="post" enctype="multipart/form-data">
        <input type="file" name="filename" />
        <input type="submit" value="Upload" />
    </form>
<br /> <br />

<p align="left"><b>Bekijk hier de gecropte en geuploadde foto's</b></p>


    <p class="album">
        <?php include 'album.php';?>
    </p>

</div>

<div id="copyright">
Copyright &copy; Kees Sonnema & Jan Beetsma
</div>

</body>
</html>

My code to crop the image with JCrop:

<html>
    <head>
    <script src="js/jquery-1.7.2.min.js"></script>
    <script src="js/lightbox.js"></script>  
    <link href="style/css/lightbox.css" rel="stylesheet" />
    </head>
<body>

<?php

$page = $_SERVER['PHP_SELF'];

//settings
$column = 6;

// directories
$base = "data";
$uploads = "thumbs";

// get album
$get_album = $_GET['album'];

if (!$get_album)
{
    echo "<b>Selecteer een album:</b><p />";
    $handle = opendir($base);
    while (($file = readdir($handle))!==FALSE)
    {
        if (is_dir($base."/".$file) && $file != "." && $file !=".." && $file !="$uploads")
        {
            echo "<a href='$page?album=$file'>$file</a><br />";
        }
    }
    closedir($handle);
}

else
{
    if (!is_dir($base."/".$get_album) || strstr($get_album,".")!=NULL || strstr($get_album,"/")!=NULL || strstr($get_album,"\\")!=NULL)
    {
        echo "Dit album bestaat niet.";
    }
    else
    {
        $x = 0;
        echo "<b>$get_album</b><p />";
        $handle = opendir($base."/".$get_album);
        while (($file = readdir($handle)) !== FALSE)
        {
            if ($file != "." && $file != "..")
            {
                echo "<table style='display:inline;'><tr><td><a href='$base/$get_album/$file' rel='lightbox'><img src='$base/$get_album/$file' height='150' width='100'></a></td></tr></table>";
                $x++;
            }
                if ($x==$column)
                {
                    echo "<br />";
                    $x = 0;
                }
            }
    }
    closedir($handle);

    echo "<p /><a href='$page'>Terug Naar Albums</a>";

}

?>

</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:20:43+00:00Added an answer on June 16, 2026 at 4:20 am

    Change the line from

    $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
    

    to

    $dst_r = imagecreatetruecolor($_POST['w'], $_POST['h']);
    

    => this will create a new image with selected area, you can delete $targ_w and $targ_h variables.

    And change the line from

    imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
    $targ_w,$targ_h,$_POST['w'],$_POST['h']);
    

    to

    imagecopy(
        $dst_r, $img_r,
        0, 0, $_POST['x'], $_POST['y'],
        $_POST['w'], $_POST['h']
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that has this format scattered around but I dont know
I have an application that has 2 beans with the same name, but which
I have an application that has lessons on British accent. This is for people
I have an application that has drag and drop functionality to import images and
I have an application that has a good look on screens, but has a
I have an application that has been running fine for quite awhile, but recently
I have an application that has iCloud enabled in the Entitlements file, but doesn't
I have an application that has Powershell 1 embedded into it, but we need
I have an application that has several objects (about 50 so far, but growing).
We have an application that has a database full of polygons (currently stored as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.